Whats the real difference between declaring an array like this:
var myArray = new Array();
and
var myArray = [];
Whats the real difference between declaring an array like this:
var myArray = new Array();
and
var myArray = [];
There is a difference, but there is no difference in that example.
Using the more verbose method: new Array()
does have one extra option in the parameters: if you pass a number to the constructor, you will get an array of that length:
x = new Array(5);
alert(x.length); // 5
To illustrate the different ways to create an array:
var a = [], // these are the same
b = new Array(), // a === b
c = ['foo', 'bar'], // these are the same
d = new Array('foo', 'bar'), // c === d
// these are different:
e = new Array(3), // e.length == 3, e[0] == undefined
f = [3] // f.length == 1, f[0] == 3
;
Here is a piece of JavaScript code that will verify that both declarations lead to the same type:
var test1 = [];
test1.push("value");
var test2 = new Array();
test2.push("value");
alert(typeof(test1) == typeof(test2));
The first one is the default object constructor call. You can use it's parameters if you want.
var array = new Array(5); //initialize with default length 5
The second one gives you the ability to create not empty array:
var array = [1, 2, 3]; // this array will contain numbers 1, 2, 3.
For more information, the following page describes why you never need to use new Array():
http://yuiblog.com/blog/2006/11/13/javascript-we-hardly-new-ya/
You never need to use new Object() in JavaScript. Use the object literal {} instead. Similarly, don’t use new Array(), use the array literal [] instead. Arrays in JavaScript work nothing like the arrays in Java, and use of the Java-like syntax will confuse you.
Do not use new Number, new String, or new Boolean. These forms produce unnecessary object wrappers. Just use simple literals instead.
Also check out the comments - the new Array(length) form does not serve any useful purpose (at least in today's implementations of JavaScript).
The difference between creating an array with the implicit array and the array constructor is subtle but important.
When you create an array using
var a = [];
You're telling the interpreter to create a new runtime array. No extra processing necessary at all. Done.
If you use:
var a = new Array();
You're telling the interpreter, I want to call the constructor "Array" and generate an object. It then looks up through your execution context to find the constructor to call, and calls it, creating your array.
You may think "Well, this doesn't matter at all. They're the same!". Unfortunately you can't guarantee that.
Take the following example:
function Array() {
this.is = 'SPARTA';
}
var a = new Array();
var b = [];
alert(a.is);
alert(b.is);
b.push('Woa');
a.push('Woa');
In the above example, the first call will alert 'SPARTA' as you'd expect. The second will not. You will end up seeing undefined. You'll also note that b contains all of the native Array object functions such as 'push', where the other does not.
While you may expect this to happen, it just illustrates the fact that '[]' is not the same as 'new Array()'.
It's probably best to just use [] if you know you just want an array. I also do not suggest going around and redefining Array...
Cheers!
Oddly enough, new Array(size)
is almost 2x faster than []
in Chrome, and about the same in FF and IE (measured by creating and filling an array). It only matters if you know the approximate size of the array. If you add more items than the length you've given, the performance boost is lost.