When using <code>new Array(2)
, you are creating an array with a size of 2, but it contains two undefined
values. On the other hand, [2]
creates an array with a size of 1, containing the number 2. In my opinion, using new Array
doesn't align with the essence of JavaScript, despite it potentially simplifying array creation. Whether this matters or not depends on personal preference. Personally, I prefer using literals for all applicable types in JavaScript, especially for maintaining large projects exceeding 30-50 KLOC.
edit Experienced JavaScript programmers tend to steer clear of the new Array
syntax due to the following reasons:
- It lacks consistency when handling arguments of different numbers and types (
(new Array(X)).length == 1
for any X
unless typeof(X) != "number"
- It is more verbose and only adds to the irregularity of the code