Possible Duplicate:
What’s the difference between “Array()” and “[]” while declaring a JavaScript array?
In JavaScript you can create a new array like:
var arr = new Array();
or like:
var arr2 = [];
What is the difference and why would you do one over the other?
new Array(2)proudces an array of size 2, containing twoundefineds.[2]produces an array of size 1, containing number 2.new ArrayIMO 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 Arraysyntax are:(new Array(X)).length == 1for anyXas long astypeof(X) != "number"