In Javascript, you may (and should) declare a variable using the var
keyword, but it's not required. So any variable can be declared like this:
var a = 'abc';
or
a = 'abc';
But the first one (with var
) should always be used when you're creating a new variable. Otherwise, you might be overwriting an already existing variable with the same name. An array is also a variable, so it too can be declared either with or without the var
keyword. Then there are two ways to declare an array, and both do exactly the same thing:
var a = ['a', 'b', 'c'];
does the same as:
var a = new Array('a', 'b', 'c');
and the new
keyword, in this case, is not required - as per the javascript specification. But it's usually used to indicate that you're creating a new instance of an object.