JavaScript » Statements » var

Syntax:
var

The var statement is used to declare a variable, and outside of a function its use is optional.

While a variable can be declared simply by assigning it a value, it is good practice to use var as there are two cases in functions where it is necessary:

  • If a global variable of the same name exists.
  • If recursive or multiple functions use variables of the same name.

 

Examples

Code:
var i;
Code:
var i = 0, x, max = 57;
Explanation:

You can also declare more than one variable and, optionally, assign values at the same time.