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

7 Answers 7

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.

Sign up to request clarification or add additional context in comments.

3 Comments

+1 I believe this is the main reason. If I recall correctly, the [] syntax was just not available in earlier JavaScript implementations.
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.
Another good reason to use [] is simply: new Array() => 11 characters; [] => 2 characters. Just use the short notation is both are pretty much equivalent.
7
  • shorter
  • arguments of Array are confusing (e.g. new Array(10) and new Array("10") are quite different)

1 Comment

+1 Even worse: new Array(10) and new Array(10, 20) are completely different.
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.

6 Comments

Also: jslint flags new Array() as a problem and suggests replacing it with [].
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()"
There are indeed no dangers if you use it consistently. It's just a "practice".
In this context I'm pretty sure they're synonyms in almost all engines.
"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"
|
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.

1 Comment

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
2

It scores you less symbols in code golf.

Comments

1

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

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.