Both forms produce the same result (unlike String
, for example, where there is difference between the literal "foo"
and the new String("foo")
object). In JavaScript, arrays are not "special" in any way - just objects with a length
parameter. Even the indexes are identical to that of a common textual object (eg, arr[0]
is the same as arr["0"]
).
This section of the ECMAScript specification describes the literal for arrays as a "object initializer", with the same effect in practice as creation via new Array
. Already this other section determines that a call to the constructor in function form ( ie without new
, only Array(params)
) has the same effect as your call in builder form. So the three forms are in fact equivalent:
var arr = new Array(element0, element1, ..., elementN);
var arr = Array(element0, element1, ..., elementN);
var arr = [element0, element1, ..., elementN];
As pointed out in the answers to the similar question in SOEN, there are some details to consider:
-
Creating an array via literal allows you to specify the initial elements at most. Creation via constructor allows specifying only the length
of the array, and nothing else (called the constructor with a single numeric argument). This is counter-intuitive and can cause unexpected errors:
var a = new Array(1); // length:1 (e mais nada)
var b = new Array(1,2); // length:2, 0:1, 1:2
var c = new Array('1'); // length:1, 0:'1'
Font
Note: If used this way, the number must necessarily be integer. This call for example throws an exception:
var d = new Array(1.5); // RangeError: Invalid array length
-
You can reset the Array
constructor however you want (even though it is no longer the Array
native), in order to customize the creation of new arrays. When you do this, literal creation is not modified:
var proto = Array.prototype;
function Array() {
this.is = 'SPARTA';
}
Array.prototype = proto;
var a = new Array();
var b = [];
alert(a.is); // => 'SPARTA'
alert(b.is); // => undefined
The same happens if you attempt to replace window.Array
(i.e. modify the global object, which in the case of browsers is window
), creation via literal is not affected. For these reasons, the creation performance via literal should be slightly larger, since there is no context checking for the variable Array
, etc.
Font
Note: As pointed out by @bfavaretto in the comments, resetting native JavaScript objects is a bad practice (aggravated by the fact that it is not exactly a redefinition that is being done, but creating an object unrelated, just with the same name, ie shadowing ).
Although the above is in compliance with the specification, particular implementations can do optimizations in either case. As noted in that related question , sometimes two constructs that at first glance seem equivalent have radically different performance in practice. The same goes for using a Array
or simply an array-like (ie a common object with a length
field).
In the absence of specific information, all that remains is testing and experimenting (as in the @Miguel Angelo answer , where it confirms that prototype
s are equal, it can be observed using Object.getPrototypeOf
in both arrays and comparing them). However, in this case the specification is clear, I believe there will be no "surprise" ...