I believe they're identical. I never use new Array();
morgancodes
2010-02-04 22:33:00
new Array(2)
proudces an array of size 2, containing two undefined
s. [2]
produces an array of size 1, containing number 2. new Array
IMO doesn't fit with the spirit of JavaScript, even though it may make array construction much more findable. That may or may not be of any importance (I use literals almost exclusively in JavaScript for all applicable types, and I've authored/maintained large pieces of JavaScript [30-50 KLOC] successfully).
edit I guess the reasons seasoned javascript programmers avoid new Array
syntax are:
(new Array(X)).length == 1
for any X
as long as typeof(X) != "number"
Another (minor) reason to use []
in preference to new Array()
is that Array
could potentially be overridden (though I've never seen it happen) and []
is guaranteed to work.
Array = "something";
var a = new Array(); // Fails
var b = []; // Works