Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
Before you use a variable in a JavaScript program, you
should declare it. Variables are declared with the
var keyword, like
this:
var i; var sum;
You can also declare multiple variables with the same var keyword:
var i, sum;
And you can combine variable declaration with variable initialization:
var message = "hello"; var i = 0, j = 0, k = 0;
If you don’t specify an initial value for a variable with the
var statement, the variable is
declared, but its value is undefined
until your code stores a value into it.
Note that the var statement can
also appear as part of the for and for/in loops (introduced
in Chapter 4), allowing you to succinctly declare the
loop variable as part of the loop syntax itself. For example: