Using bracket notation, you can initialize an array with zero or more values:
var a= []; //length: 0, no items
var a= [1]; //length: 1, items: 1
var a= [1,2]; //length: 2, items: 1,2
Using new Array()
, you can initialize an array with zero or two or more values:
var a= new Array(0); //length: 0, no items
var a= new Array(1); //length: 1, items: undefined
var a= new Array(1,2); //length: 2, items: 1,2
Referring to the multiple-parameter syntax, in JavaScript: The Definitive Guide, Flanagan writes:
Using an array literal is almost always simpler than this usage of the Array() constructor.
He doesn't provide any examples in which the multiple-parameter syntax is simpler, and I can't think of any. But the words, "almost always" imply that there may be such instances.
Can you think of any?
Note that I understand the difference between the methods. My question specifically is, Why would you ever use the multiple parameter syntax with new Array()
? Flanagan implies that there may be a reason.