JavaScript tutorial:
var statement

 

The var statement declares a variable.

Syntax

var variable [ = value ] [, variable2 [ = value2], ...]

The var statement syntax has the following parts:

Part

Description

variable, variable2

The names of the variables being declared.

value, value2

The initial value assigned to the variable.

Return value

None

Example

Use the var statement to declare variables. These variables can be assigned values at declaration or later in your script. Examples of declaration follow:

function declareVar()
{
var index = 100;
document.write("var index: " + index);
var name = "Thomas Jefferson";
document.write("var name: " + name);
}
document.write(declareVar());

To run the code, paste it into JavaScript Editor, and click the Execute button.

See Also: function Statement, new Operator