tags:

views:

328

answers:

7

What are the advantages of using

var foo = []; 

over using

var bar = new Array();

i've been told to use [] over new Array() but never with much explanation

+6  A: 
  • shorter
  • arguments of Array are confusing (e.g. new Array(10) and new Array("10") are quite different)
stereofrog
+1 Even worse: new Array(10) and new Array(10, 20) are completely different.
Álvaro G. Vicario
+1  A: 

It's the same thing. The only reason to use [] over new Array() is that it's shorter.

reko_t
+1  A: 

shorter and cleaner. Should use {} to create objects as well.

BrennaSoft
+3  A: 

Both can be used as good. This discussion/confusion has started since Javascript guru Douglas Crockford told that the new keyword is considered harmful. Since then it was considered "good practice/technique" to leave out the new keyword to avoid unexpected results/behaviour. Also see this Stackoverflow topic.

BalusC
Also: jslint flags `new Array()` as a problem and suggests replacing it with `[]`.
Cheeso
The top answers at that link state that it is not harmful, and that there are many instances where it could be used. Furthermore, I see no danger or harm in "new Array()"
Sasha
There are indeed no dangers if you use it consistently. It's just a "practice".
BalusC
In this context I'm pretty sure they're synonyms in almost all engines.
Álvaro G. Vicario
"The new keyword is considered harmful." is no more useful than saying "use [] over new Array()". The question asked for explanations not rules.I like the response "shorter and cleaner"
plodder
The potential harmfulness of `new` keyword as stated by Douglas Crockford is the origin of this all. I've edited the answer to include more detail.
BalusC
+19  A: 

The main reason to use [] as opposed to new Array() is the arguments you pass. When you use new Array(10), you create an empty array with 10 elements, but when you use [10], you create an array with one element who's value is 10. Since this is generally the information most programmers want to pass to an array (as arrays are dynamic in Javascript), this is generally considered the best practice. Also new Array(10,20) works differently than new Array(10). In this case you have the same effect as [10,20] which is to create an array with 2 elements, 10 and 20. Since this is... strange at best... it's easy to accidentally create an empty array by passing new Array() only one value. [] always has the same effect, so I'd also recommend sticking with it.

Chibu
+1 I believe this is the main reason. If I recall correctly, the [] syntax was just not available in earlier JavaScript implementations.
Álvaro G. Vicario
Indeed, array and object literals were introduced in JavaScript 1.2. IE 4 and Netscape 4 both supported array and object literals, so both are entirely safe to use these days unless for some reason you need to cater for earlier versions of those browsers.
Tim Down
Two other reasons, cleaner syntax (yes and as you mentioned the Array constructor is not very intuitive). Also, the literal seems to be 25% faster. Try this in firebug, or chrome`var a;``console.time('literal');``for (var i=0; i < 1000000; i ++) a = [];``console.timeEnd('literal');``console.time('ctor');``for (var i=0; i < 1000000; i ++) a = new Array();``console.timeEnd('ctor');`
Juan Mendes
+1  A: 

It scores you less symbols in code golf.

Kuroki Kaze
+2  A: 

Three reasons (the first two expanded upon in other answers):

  1. Shorter;
  2. Allows creation of arrays with one element, as detailed in Chibu's answer;
  3. Works even in the unlikely event of the Array constructor having been overwritten.

Non-reasons:

  1. Avoidance of the new operator. Don't be afraid of new just because Douglas Crockford was once bitten by it. It's extremely useful.
Tim Down