2

Possible Duplicate:
What’s the difference between “Array()” and “[]” while declaring a JavaScript array?

What is the difference between these two methods of defining an array in javascript ?

var arrayList = []

and

var arrayListAgain = new Array();
Community
  • 1
  • 1
thiswayup
  • 2,069
  • 8
  • 32
  • 52
  • 3
    Duplicate? http://stackoverflow.com/questions/931872/whats-the-difference-between-array-and-while-declaring-a-javascript-arr – Anders Jul 27 '10 at 21:44
  • One is pretty, one is ugly. One is an array literal, one creates a new Array object. They pretty much do the same thing... I've never seen a use for `new Array(countOfUndefinedElements);` though I guess there will be some. – Warty Jul 27 '10 at 21:58
  • Using the literal initialization `[]` is insignificantly faster ;) – Felix Kling Jul 27 '10 at 22:19

4 Answers4

4

One is good and the other is bad :-)

No seriously, the first way is shorter (fewer bytes sent over the wire) and it doesn't have any weird potential problems. The second one, however, will work fine.

The weird part with new Array() is this: if you pass one numeric parameter to the constructor, it means "initialize the array so that there are that there are the given number of empty (null) array elements, and so that length is equal to the given number." (Well it has to be an integer, I think actually.) If you pass a single non-number, or two or more numbers, it means, "initialize the array so that its contents reflect this argument list."

Ok now I said there was a weird part, and it's this: if you're using some sort of server side framework, and you want to create an array of numeric values (integer values), like say unique database keys, then you might be tempted to do this:

var keys = new Array(<@ favoriteTemplateLanguage.forEach(idList): print(id + ',') @>);

(I made up that syntax of course.) Now what happens if there's just one "id" in your server-side list of keys?

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • Yes don't over work the wires! – Jonathan Park Jul 27 '10 at 21:43
  • .. so which one is good and which one is bad? It seems like you complimented both... – Hristo Jul 27 '10 at 21:43
  • I was kidding about the "bad" part, but in my opinion the first notation ([]) is preferable because it's less quirky. – Pointy Jul 27 '10 at 21:46
  • 1
    It has be an *unsigned* integer, to be more precise, see the [ECMAScript spec](http://ecma262-5.com/ELS5_Section_15.htm#Section_15.4.2.2): “If the argument *len* is a Number and ToUint32(*len*) is equal to *len*, then the **length** property of the newly constructed object is set to ToUint32(*len*). If the argument *len* is a Number and ToUint32(*len*) is not equal to *len*, a **RangeError** exception is thrown.” – Marcel Korpel Jul 27 '10 at 21:48
  • Yes, I read that the other day on kangax's blog - that's all correct. I don't ever use the `new Array()` form so I don't think about it too much :-) – Pointy Jul 27 '10 at 22:37
2

You can use the second one to define an array of a predefined length, e.g.

var arrayListAgain = new Array(20);

will create an array with 20 (undefined) elements.

Also see new Array (len).

Marcel Korpel
  • 21,536
  • 6
  • 60
  • 80
1

none what so ever. Both create a new instance of an Array object.

Douglas Crockford in 'Javascript the good parts' recommends the former method as it is more concise.

Gareth Davis
  • 27,701
  • 12
  • 73
  • 106
1

They are the same. According to http://www.hunlock.com/blogs/Mastering_Javascript_Arrays:

Current best-practice eschews the "new" keyword on Javascript primitives. If you want to create a new Array simply use brackets [] like this… var myArray = [];

I recommend reading that page, it contains a lot of useful information about arrays and JS in general.

Karel Petranek
  • 15,005
  • 4
  • 44
  • 68