3

Is there any difference between these three ways of creating an array? If not, why do the first two ways exist if they're just more code?

1:

var myCars=new Array(); 
myCars[0]="Saab";       
myCars[1]="Volvo";
myCars[2]="BMW";

2:

var myCars=new Array("Saab","Volvo","BMW");

3:

var myCars=["Saab","Volvo","BMW"];
Eric
  • 2,004
  • 6
  • 23
  • 33
  • 5
    They all serve the same purpose. `[]` creates an array in the same way `new Array()` does. Populating the values in that same line vs doing it in a separate line for each also makes no difference other than maintainability. (if you had to add a new one at the start, you'd have to renumber the rest.). I'm not sure how to answer the 2nd half of your question. – Kevin B Mar 12 '13 at 18:06
  • The first two ways exist because they’re possible (#1 as a side-effect), however unfortunate that may be. – Ry- Mar 12 '13 at 18:07
  • 2
    @KevinB They are not created in the "same" way. See http://stackoverflow.com/questions/3500115/difference-between-new-array-and-in-javascript and http://stackoverflow.com/questions/931872/whats-the-difference-between-array-and-while-declaring-a-javascript-ar – Oleg Mar 12 '13 at 18:12
  • Literal Array Notation (your number 3, or []) is the most commonly accepted notation, as jslint prefers it, and jslint is the most popular javascript code quality checker. It is also the most minimal if bandwidth is an issue. – howderek Mar 12 '13 at 18:13
  • You can also create an array of length n with var a=Array(n), no 'new' needed. This can be handy for running an iterator (like map or forEach) if you are building a complex array. – kennebec Mar 12 '13 at 18:34

3 Answers3

6

Using [] is more secure and reliable than using new Array(). The former is actually recommended. This is because the value of Array can be overridden. Also, [] is faster compared to new Array().

Take a look at these related questions:

A related link:

To clarify what "overridden" means, you can do something like this:

function Array() {
    alert("I am not really an array! BWAHAHAHAHAHA!");
}

Now when you do new Array(); you will get an alert, which is obviously not what you want.

In short, there really is no pressing need to to use new Array() and it doesn't buy you anything more compared to using the literal [].

Community
  • 1
  • 1
Vivin Paliath
  • 94,126
  • 40
  • 223
  • 295
1

Difference:

In 1st method, array declaration and array initialization are done separately. In 2nd and 3rd method, array declaration and array initialization is done in a single statement. 3rd is just a shorthand for 2nd method.

Need:

Case 1: Let's say you have no cars today, but you intend to buy one in the future.So,

var myCars = new Array();

Case 2: Let's say you own 2 cars today. But in future, you might buy/sell one or more cars.

var myCars = new Array("BMW", "Porsche");

To conclude, if you know the data before hand, you use 2nd or 3rd method but if you don't know the data before hand, you use 1st method.

Girish
  • 883
  • 8
  • 16
  • This doesn't really explain the difference between `[]`, and using `new Array()`. – Vivin Paliath Mar 12 '13 at 18:20
  • @VivinPaliath - agreed. But the first part of question is on difference between 3 ways/syntax of creating arrays not which one is better? I don't have much knowledge into JS internals... – Girish Mar 12 '13 at 18:27
0

For Arrays there is not a significant difference between the 2 approaches, other than readability and a very slight performance advantage for the plain notation.

However the general recommended practice is to use the plain notation when possible for javascript rather than wrapping these things in a constructor. Because while 'new Array()' and 'new Object()' produce the same as [] and {}, using constructors for primitives actually can cause issues.

For instance new String() will create a String object rather than a primitive string. This has the disadvantage of preventing the use of === and other comparisons on the string.

For example

 alert(new String("") === new String("")); //false alert ("" === "")
 //true

So while there is no difference in this particular case, in general its better to get in the habit of using the basic notation rather than using constructors for built in types.

Clarification

When I say no difference, I mean that in the general case those notations will produce the same result. Vivin's points are excellent and are another reason for avoiding this notation.

Ben McCormick
  • 25,260
  • 12
  • 52
  • 71
  • -1: There are significant differences other than readability! `Array` can be overridden whereas `[]` cannot. – Vivin Paliath Mar 12 '13 at 18:18
  • There is a difference. In array construction and in functionality. For example, `new Array(10)` is not the same as `[10]`. – Oleg Mar 12 '13 at 18:18
  • @Oleg true, but that wasn't one of his examples. – Ben McCormick Mar 12 '13 at 18:20
  • @VivinPaliath Its true that the first notation provides more opportunities to be stupid, but thats not going to be a significant difference in most cases. I upvoted your answer as well, I found it interesting, but in the general case these notations will produce the same value. I was looking at why the new notation for built in types can cause issues in general. I think our answers are both adding useful but separate contributions. – Ben McCormick Mar 12 '13 at 18:25
  • @ben336 I didn't downvote your answer :). But I think `new Array()` causes far more issues than it's worth because it doesn't provide anything more than what `[]` provides. In fact, it has the potential to cause more issues. This is why I vastly prefer `[]` over `new Array()` because IMHO seeing `[]` is enough for me to realize that it is an array. – Vivin Paliath Mar 12 '13 at 18:27
  • @VivinPaliath We're not disagreeing. Your reasons are great, and you deserve the upvotes. I just decided to focus on some other reasons why that notation can be generally harmful rather than going all over your reasons again. – Ben McCormick Mar 12 '13 at 18:30