3

I am new to JavaScript. From some tutorials I know in javascript there are two ways to declare the array. One is like this:

var test = new Array("apple","pine");

or

test = new Array("apple","pine");

Two are like this,

var test=["apple","pine"];

but when i use this way to declare it:

test=Array("apple","pine");

It is still ok. why?

Herman Schaaf
  • 46,821
  • 21
  • 100
  • 139
zhuanzhou
  • 2,409
  • 8
  • 33
  • 51
  • Wait a minute ... is this question about leaving out `var` or about leaving out `new`? – Šime Vidas Apr 28 '11 at 14:53
  • have a look at [Difference between using var and not using var in JavaScript](http://stackoverflow.com/questions/1470488/difference-between-using-var-and-not-using-var-in-javascript) – Gabriele Petrioli Apr 28 '11 at 14:53

4 Answers4

3

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.

Herman Schaaf
  • 46,821
  • 21
  • 100
  • 139
  • That is not actually true all the time, and leaving off `var` is a terribly bad idea in general. – Pointy Apr 28 '11 at 14:49
  • Agreed, I should probably clarify that. Tried to keep the explanation as simple as possible. – Herman Schaaf Apr 28 '11 at 14:50
  • 1
    Yes ok that's cool - leaving off `var` is a huge pitfall for programmers new to JavaScript, because it can make weird things happen that are quite difficult to debug. Scoping in JavaScript is confusing enough anyway :-) – Pointy Apr 28 '11 at 14:52
  • 1
    Leaving out the `new` keyword can also create problems with any constructor (not only Array) cause `this` will not be bound to a new object but to the global object. – Josmas Apr 28 '11 at 15:12
  • To clarify, leaving off the var creates a global variable (which is why you may be overwriting an existing variable) – Matt Apr 28 '11 at 16:10
2

Because new Array(...) and Array(...) do the same thing (= create a new array). That's just how it's defined in the spec.

See here: http://es5.github.com/#x15.4.1

When Array is called as a function rather than as a constructor, it creates and initialises a new Array object. Thus the function call Array(…) is equivalent to the object creation expression new Array(…) with the same arguments.

Therefore, these 3 lines are equivalent:

arr = ['apple', 'pine'];
arr = new Array('apple', 'pine');
arr = Array('apple', 'pine');
Šime Vidas
  • 182,163
  • 62
  • 281
  • 385
2

First, you really should stick to the simple "[ ... ]" notation to create and initialize your arrays. Thus:

var test = ["apple", "pine"];

You should also be careful to use var for all your local variables. (Actually I'll go out on a limb and say that it's just bad practice not to use var for all declarations.)

Now, using the Array constructor without the new prefix works because that's just how the Array constructor is defined. In other words, if it's not invoked with new, it returns you an array anyway. However, you really shouldn't worry about it because in most circumstances there's no reason to use the Array constructor at all.

Pointy
  • 405,095
  • 59
  • 585
  • 614
2

var is used to declare a variable's scope.

x = 'hello';

function y() {
    x = 'goodbye';
}

function z() {
    var x = 'how are you';
}

y()
alert(x); // outputs 'goodbye';
z();
alert(x); // outpus 'hello';

Basically var declares a variable to be local scope. It has no real effect if you use it at the top level of a script, but within a function it'll make the variable "local".

Marc B
  • 356,200
  • 43
  • 426
  • 500