Jun 12, 2012

Javascript Variables


JavaScript is an untyped language. This means that a variable can hold any datatype. It also means that a variable may be assigned data of one particular datatype and then later be given data of a different datatype.

Definition

Variables are used to hold values or expressions. They can be thought of as named containers. You can place data into these containers and then refer to the data simply by naming the container.


Declaring Variables

You declare JavaScript variables with the var keyword
<script type="text/javascript">
var age;
var name;
</script>

After the declaration shown above, the variable is empty.
You can also declare multiple variables with the same var keyword as follows:
<script type="text/javascript">
var age, name;
</script>

When using a variable for the first time it is not necessary to use "var" before the variable name, but it is a good programming practice. Use the var keyword only for declaration or initialization once for the life of any variable name in a document. You should not re-declare same variable twice.

Rules for naming variables

  • You should not use any of the JavaScript reserved keyword as variable name.
  • They must begin with a letter (not digit), the underscore character or currency. For example, 123test is an invalid variable name but_123test is a valid one.
  • JavaScript variable names are case sensitive.
  • After the first character, can contain letters, digits, and underscores
  • Cannot contain space


JavaScript Reserved Words:

The following are reserved words in JavaScript. They cannot be used as JavaScript variables, functions, methods, loop labels, or any object names.

abstract
boolean
break
byte
case
catch
char
class
const
continue
debugger
default
delete
do
double
else
enum
export
extends
false
final
finally
float
for
function
goto
if
implements
import
in
instanceof
int
interface
long
native
new
null
package
private
protected
public
return
short
static
super
switch
synchronized
this
throw
throws
transient
true
try
typeof
var
void
volatile
while
with



Assignation initialization

Storing a value in a variable is called variable initialization. You can do variable initialization at the time of variable creation or later point in time when you need that variable using the equal (=) sign
If you assign values to variables that have not yet been declared, the variables will automatically be declared as global variables.
<script type="text/javascript">
var name = "Ali";
var age;
age = 2000.50;
</script>

Special variable values

undefined

If a variable hasn't been declared or assigned yet then that variable will be given a special undefined value. Evaluates to false in boolean operations. Always use === when comparing to undefined.
var x;
var y = false;

typeof(x) → undefined (as a string)

x == y → true
x === y → false

NaN

Not a Number, generated when arithmetic operations return invalid results. Evaluates to false in boolean operations. Always use isNaN() when comparing to NaN. NaN is never equal to itself!
var x = 10/seventeen;

x → NaN

var x = 10/seventeen;

x == x → false


Scope

The scope of a variable is the region of your program in which it is defined. JavaScript variable will have only two scopes.
Global Variables: A global variable has global scope which means it is defined everywhere in your JavaScript code.
Local Variables: A local variable will be visible only within a function where it is defined.
JavaScript does not have block scope, so defining variables in blocks can confuse programmers who are experienced with other C family languages. Define all variables at the top of the function.

Hiding variables

Within the body of a function, a local variable takes precedence over a global variable with the same name. If you declare a local variable or function parameter with the same name as a global variable, you effectively hide the global variable.

Local Variables

A variable declared within a JavaScript function becomes LOCAL and can only be accessed within that function.
Function parameters are always local to that function.
You can have local variables with the same name in different functions, because local variables are only recognized by the function in which they are declared.
Local variables are deleted as soon as the function is completed.

Global variables

Variables declared outside a function, become GLOBAL, and all scripts and functions on the web page can access it.
Global variables are deleted when you close the page.
If you assign values to variables that have not yet been declared, the variables will automatically be declared as global variables.
Use of global variables should be minimized. Implied global variables should never be used.

Naming Conventions

Names should be formed from the 26 upper and lower case letters (A .. Z, a .. z), the 10 digits (0 .. 9), and _ (underbar).
Avoid use of international characters because they may not read well or be understood everywhere. Do not use $ (dollar sign) or \ (backslash) in names.
Do not use _ (underbar) as the first character of a name. It is sometimes used to indicate privacy, but it does not actually provide privacy.
Most variables and functions should start with a lower case letter.
Global variables should be in all caps.
All variables should be declared before used. JavaScript does not require this, but doing so makes the program easier to read and makes it easier to detect undeclared variables that may become implied global. Implied global variables should never be used.
The var statements should be the first statements in the function body.
It is preferred that each variable be given its own line and comment. They should be listed in alphabetical order.



1 comment: