Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between "new Array(..)" and "[..]" in JavaScript? [duplicate]

Is

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

and

var myCars=["Saab","Volvo","BMW"];

exactly the same ?

like image 262
Misha Moroshko Avatar asked Aug 17 '10 07:08

Misha Moroshko


1 Answers

Yes, for that case they are the same.

There is a difference if you have only one item, and it's numeric. This will create an array with a single item:

var myNumbers = [42];

but this will create an array with the length 42:

var myNumbers = new Array(42);
like image 157
Guffa Avatar answered Sep 21 '22 17:09

Guffa