125

What is the difference (if there is any) between

x = Array()

and

x = new Array()

Which one should I use?

1

4 Answers 4

140

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.

43

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.

2
  • 52
    Actually, new Array(5) gives [,,,,] Mar 21, 2018 at 17:50
  • 2
    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 8, 2021 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().

8
  • Correct, there are the exact same.
    – Raynos
    Nov 20, 2011 at 23:35
  • 4
    You should probably use x = [ ] in both JavaScript and CoffeeScript unless you need to pre-size the array for some reason. Nov 20, 2011 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). Nov 21, 2011 at 0:33
  • @minitech - why the "should"?
    – RobG
    Nov 21, 2011 at 0:52
  • 2
    @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, 2011 at 0:52
8

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]
6
  • that part about object literal [] being the same as Array and new Array is not true am afraid. Read more here: stackoverflow.com/questions/931872/…
    – azrahel
    Oct 13, 2020 at 9: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:[]} Oct 13, 2020 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, 2020 at 11:07
  • @azrahel It's no doubt that performance may differ, but technically each do the same thing. Ref: developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/… , developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/… Oct 16, 2020 at 12:29
  • 1
    @Melab what is the 1 %? Sep 8, 2021 at 7:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.