102

What is the difference (if there is any) between

x = Array()

and

x = new Array()

Which one should I use?

j08691
  • 197,815
  • 30
  • 248
  • 265
scravy
  • 11,355
  • 14
  • 67
  • 119
  • 2
    You might find this interesting: http://stackoverflow.com/questions/383402/is-javascript-s-new-keyword-considered-harmful – nickd Nov 20 '11 at 23:38

4 Answers4

117

The spec says:

When Array is called as a function rather than as a constructor, it creates and initialises a new Array object. Thus the function call Array(…) is equivalent to the object creation expression new Array(…) with the same arguments.

Itay Maman
  • 29,207
  • 10
  • 83
  • 115
SLaks
  • 837,282
  • 173
  • 1,862
  • 1,933
33

You should use the literal []. Reasons are outlined here. Using the Array() constructor can be ambiguous, since it accepts either a length or a list of elements:

new Array(5)   // []
new Array('5') // ['5']

[5]   // [5]
['5'] // ['5']

The reason you can use Array without the new operator is that internally it does a common trick with constructors:

function Thing(){
    if (!(this instanceof Thing)){
        return new Thing()
    }
    // ... define object
}

That is, if you call Thing() it will call new Thing() for you.

Ricardo Tomasi
  • 33,240
  • 2
  • 54
  • 66
  • 47
    Actually, ```new Array(5)``` gives ```[,,,,]``` – Stefan Octavian Mar 21 '18 at 17:50
  • In ES5, To avoid this issue, you can use `Array.of`: "Array.of(7) creates an array with a single element, 7, whereas Array(7) creates an empty array with a length property of 7" – Rsh Aug 08 '21 at 14:11
9

I believe that both are equivalent. However, in JavaScript at least, you should always use the literal syntax:

x = []

But based on some tests in the browsers I have, Array(1, 2, 3) gives the same result as new Array(1, 2, 3), and same with Array(15) and new Array(15). Or just plain new Array().

Ry-
  • 209,133
  • 54
  • 439
  • 449
  • Correct, there are the exact same. – Raynos Nov 20 '11 at 23:35
  • 3
    You should probably use `x = [ ]` in both JavaScript and CoffeeScript unless you need to pre-size the array for some reason. – mu is too short Nov 20 '11 at 23:39
  • mu is exactly right: Use `arr = new Array(n)` (where `n` is a number) if and only if you're doing something performance-intensive where you know how large the array will be in advance, so the required memory is allocated all at once (in principle). – Trevor Burnham Nov 21 '11 at 00:33
  • @minitech - why the "should"? – RobG Nov 21 '11 at 00:52
  • 1
    @TrevorBurnham - there is no reason to believe that setting the length property will cause any memory to be allocated or improve performance. – RobG Nov 21 '11 at 00:52
  • @RobG: I don't understand... but I'll give the two possible responses. 1) `new Array` is still valid in JavaScript, it's not as if you can't use it, and as TrevorBurnham points out, it may be beneficial in some cases (yes, this is true on some browsers). 2) `[]` is shorter and much clearer, in my opinion - it specifies a list and the syntax is the same in many other languages. `new Array()` feels like `new String()` and `new Number()` which are usually incorrect. – Ry- Nov 21 '11 at 00:56
  • @minitech - because whenever someone says you *should* do something, you need to know their criteria so that you can determine whether it is suitable in your case. Another reason for not using the array constructor is that the result of `new Array(9)` and `new Array(9, 10)` are very different and perhaps unexpected. – RobG Nov 21 '11 at 04:02
  • @RobG: I understand that :) It's just that you could have meant "without question." – Ry- Nov 21 '11 at 17:29
4

Some facts that worth to mention:

Array === Array.prototype.constructor //true

and

new Array() does the same as new Array and [] as well

However, the result of calling a constructor is not necessarily equivalent to creating a new instance of an object. Example:

Foo = function(){}

x = Foo()   // undefined
y = new Foo // {}

So x and y can be different.

But if the Object itself is an Array you will get the same by definition, as mentioned earlier.

x = Array()   // []
y = new Array // []

Even if you pass one integer (telling the length)

x = Array(3)     // [empty × 3]
y = new Array(3) // [empty × 3]

or one non integer (telling the content)

x = Array(true)     // [true]
y = new Array(true) // [true]

or more parameters (telling the content)

x = Array(1,2,3)     // [1,2,3]
y = new Array(1,2,3) // [1,2,3]
Alex Szücs
  • 365
  • 3
  • 9
  • that part about object literal `[]` being the same as `Array` and `new Array` is not true am afraid. Read more here: https://stackoverflow.com/questions/931872/what-s-the-difference-between-array-and-while-declaring-a-javascript-ar – azrahel Oct 13 '20 at 09:39
  • @azrahel you will get the same empty array using any of above expression (if `Array` is native), try: `{a:new Array,b:new Array(),c:[]}` – Alex Szücs Oct 13 '20 at 15:13
  • no you will not :) just read the link or documentation. Using object literal `[]` takes some shortcuts and internally does not execute some code that constructor does. If you don't care about performance or other edge case issues, sure, use whichever. If you do care though, you should know about differences, cause there are some. So what am saying is 'yes' you will get empty array in each case, but 'no' it s not going to be the same structurally/internally. – azrahel Oct 15 '20 at 11:07
  • @azrahel It's no doubt that performance may differ, but technically each do the same thing. Ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Grammar_and_types#Array_literals , https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects#Using_object_initializers – Alex Szücs Oct 16 '20 at 12:29
  • That `Array === Array.prototype.constructor` evaluates to `true` is completely uninteresting and holds for 99% of all JavaScript functions. – Melab Sep 07 '21 at 12:09
  • 1
    @Melab what is the 1 %? – Alex Szücs Sep 08 '21 at 07:18