40

In JSLint, it warns that

var x = new Array();

(That's not a real variable name) should be

var result = [];

What is wrong with the 1st syntax? What's the reasoning behind the suggestion?

Chris S
  • 62,476
  • 49
  • 214
  • 238

5 Answers5

70

It's safer to use [] than it is to use new Array(), because you can actually override the value of Array in JavaScript:

Array = function() { };

var x = new Array();
// x is now an Object instead of an Array.

In other words, [] is unambiguous.

Dan Lew
  • 81,251
  • 29
  • 178
  • 174
  • Good point! Note that overriding Array will also lead to serious problems in code that attempts to use Array.prototype methods on array-like objects such as arguments... So still a very bad idea. – Shog9 May 19 '09 at 21:51
  • 2
    Not only that, but the Array() constructor has some "odd" behaviour depending on how many arguments you pass it. It's just generally quirky and best avoided. – Dominic Mitchell May 19 '09 at 22:49
  • 1
    Don't get me wrong, I'm most certainly not promoting overriding Array. (Well, not unless you override 'undefined' and 'NaN' as well, just to screw with anyone who would ever dare use your code.) – Dan Lew May 20 '09 at 00:36
  • If you override Array, what happens to var x = []; That is an Array object is it not? – Chris S May 20 '09 at 22:09
  • 4
    [] will always be an Array object. When I say "override", I don't mean to imply that the actual Array class is overridden. "Array" is actually a global variable, and as such can be assigned a new value - so I'm reassigning what the variable "Array" references. What Array used to reference is still intact. (This is also why I joke about overriding 'undefined' and 'NaN' as well - they, too, are global variables that can be manipulated in this same, twisted way.) – Dan Lew May 20 '09 at 22:21
27

Crockford doesn't like new. Therefore, JSLint expects you to avoid it when possible. And creating a new array object is possible without using new....

Community
  • 1
  • 1
Shog9
  • 146,212
  • 34
  • 221
  • 231
  • 3
    However, jslint breaks when you try to initiate an array with n values. For instance: var x = new Array(20); This method of creation isn't used often, but it has its cases and is much faster than using a loop to create and populate an array. – Phillip B Oldham Jun 02 '09 at 10:34
  • 3
    You can just set the n'th element to undefined and it does more or less the same thing – mikelikespie Apr 17 '10 at 00:38
  • 7
    There is no need to initialize an array to a certain size at declaration, though. JavaScript arrays are not like C arrays. They are Objects, not statically-sized, contiguous chunks of memory. Because of this, you can just append values as necessary to your array. In fact, it could be *slower* to initialize at declaration and later populate because at declaration time, all n key have to be hashed, then as they are assigned values, they may have to be rehashed. – Joshua Clark Aug 03 '12 at 17:55
7

It seems like you can get different performance based on which you are using and for what purpose depending on browser or environment:

http://jsperf.com/new-array-vs-literal/11 ( [1,.2] vs new Array(1,.2) ) the literal is way faster in this circumstance.

http://jsperf.com/new-array-vs-literal/7 ( new Array(500000) vs [].length(500000) ) new Array is faster in chrome v21 it seems for this test by about 7% or 30%) depending on what you do.

Tivie
  • 18,374
  • 5
  • 54
  • 77
Parris
  • 16,312
  • 15
  • 82
  • 125
5

Nothing wrong with either form, but you usually see literals used wherever possible-

var s='' is not more correct than var s=new String()....

kennebec
  • 94,076
  • 30
  • 99
  • 125
2

There's nothing wrong with the first syntax per se. In fact, on w3schools, it lists new Array() as the way to create an array. The problem is that this is the "old way." The "new way", [] is shorter, and allows you to initialize values in the array, as in ["foo", "bar"]. Most developers prefer [] to new Array() in terms of good style.

Rudd Zwolinski
  • 23,948
  • 17
  • 54
  • 60
  • 22
    probably because w3 schools is not associated w3.org and they often have poor or inaccurate code. – Andrew Austin Jul 09 '09 at 12:57
  • @AndrewAustin True. Although in the interwebs, as a general rule of thumb, you should double check the sources, regardless where you took the information from. – Tivie Jan 21 '13 at 20:29