About the Var Statement

The var statement is used to declare variables. This statement can be used to declare a single variable using the following format:

var name ;

You can use var to declare more than one variable at a time using the following format:

var name1, name2,... ;

It can also be used with initializers to declare and initialize variables in a single statement:

var name=expression;
var name1=expression1,name2=expression2,...;

Following are some examples:

var x;
var name,age;
var name="John",age=5;
var x=0,y=1,z;
var square=x*x;

If you do not specify an initializer, the variable is set equal to null:

var x;

is the same as:

var x=null;

You can also use var inside for and for/in statements as follows:

for(var n=0; n<10; n++);
for(var name in object);

See About the For Statement.

See About the For/In Statement.