19

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

Gumbo
  • 643,351
  • 109
  • 780
  • 844
Alex
  • 2,513
  • 5
  • 27
  • 42

7 Answers7

32

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,355
  • 7
  • 12
  • 4
    +1 I believe this is the main reason. If I recall correctly, the [] syntax was just not available in earlier JavaScript implementations. – Álvaro González Mar 25 '10 at 16:21
  • 2
    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 Mar 25 '10 at 22:46
  • Another good reason to use [] is simply: `new Array()` => 11 characters; `[]` => 2 characters. Just use the short notation is both are pretty much equivalent. – laurent Sep 06 '11 at 15:39
7
  • shorter
  • arguments of Array are confusing (e.g. new Array(10) and new Array("10") are quite different)
user187291
  • 53,363
  • 19
  • 95
  • 127
3

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.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 1
    Also: jslint flags `new Array()` as a problem and suggests replacing it with `[]`. – Cheeso Mar 25 '10 at 15:58
  • 7
    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 Mar 25 '10 at 16:00
  • 2
    There are indeed no dangers if you use it consistently. It's just a "practice". – BalusC Mar 25 '10 at 16:16
  • 1
    In this context I'm pretty sure they're synonyms in almost all engines. – Álvaro González Mar 25 '10 at 16:19
  • 2
    "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 Mar 25 '10 at 16:27
  • 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 Mar 25 '10 at 16:35
3

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
  • 318,141
  • 75
  • 454
  • 536
  • I'll vote up anybody who says "Don't blindly listen to Douglas Crockford". He has great ideas, but he's not God and I particularly disagree with many of them – Ruan Mendes Jun 19 '12 at 19:46
2

It scores you less symbols in code golf.

Kuroki Kaze
  • 8,161
  • 4
  • 36
  • 48
1

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

reko_t
  • 55,302
  • 10
  • 87
  • 77
1

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

Robby Pond
  • 73,164
  • 16
  • 126
  • 119