Variable statements initialize variables. Variable names, which can be any legal identifier, must be unique within a single function. The scope of the declared variable is the rest of the current function; the expression is evaluated every time the variable statement is executed.
Variables can be initialized either to a specified value or, by default, to an empty
string ("").
The following are examples of variable statements:
function count(str) {
var result = 0; // Initialized once
while(str != "") {
var ind = 0; // Initialized every time
// modify string
}
return result
}
function example(param) {
var a = 0;
if (param > a) {
var b = a+1; // Variables a and b can be used
} else {
var c = a+2; // Variables a, b, and c can be used
}
return a; // Variables a, b, and c are accessible
}