Changes

Jump to: navigation, search

Introducing JavaScript Variables

940 bytes added, 18:24, 11 April 2007
Variable Declaration and the Assignment Operator
== Variable Declaration and the Assignment Operator ==
 
Varaibles in javaScript are declared by by specifying the name name of the variable storage you wish to create prefixed with the token ''var''. For example, to create a variable named myVariable:
 
<pre>
var myVariable;
</pre>
 
The above line creates a new variable which can be accessed using the name ''myVariable'' but does not assign any initial value.
 
Note that whilst it is also possible to declare a variable without the ''var'' token this is strongly discouraged. Failing to specify the 'var' before the variable can cause confusion when variables with the same name are present in local and global scopes. Good practice dictates that all variable declarations be prefixed with the ''var'' token.
 
The '=' assignment operator can subsequently be used to assign a new value to our variable:
 
<pre>
myVariable = 10;
</pre>
 
In this instance the variable which had already declared is asssigned the to contain an integer value of 10.

Navigation menu