views:

349

answers:

6

I remember reading somewhere (I think it was in one of Crockford's papers) that using an array literal [] is better than using the new Array(); notation.

But I can't really remember any advantages of one over the other.

Can anyone please explain to me on why the former is preferred over the latter?

[Update]

Here is a reason on why [] is better than new Array();:

var Array = function () { };

Overriding the Array object will break code...!

Any more reasons ?

+22  A: 

Brevity

It has less bytes to transfer over the wire, less bytes to interpret, less mental resources to parse it.

Less is more.

Consistency

What is the difference between these two lines of code?

var arr = [5];
var arr = new Array(5);

According to here new Array(5); will not return an array with a 5 in it, instead it will return a 5 element array, with all the elements being undefined. Whereas these two lines return identical arrays.

var arr = [5,6];
var arr = new Array(5,6);
barkmadley
only brevity...?
Andreas Grech
Ugh, that constructor thing is awful. Never noticed that before.
JW
+1 for Brevity and Consistency
0A0D
A: 
  1. Javascript is a language that is parsed, not compiled. Thus if you write [] instead of new Array(); The length to parse is shortened.

  2. It is much shorter and understood to write [] instead of new Array();

thephpdeveloper
point 1 is nonsense. You have no idea what you're talking about. Parsing is something that happens with all programming languages, compiled or not, and there is virtually no difference between [] and Array(); as far as the parser is concerned.
Breton
@Brenton: I think he means that it's shorter to parse. I believe he's right
the_drow
"language that is parsed, not compiled" _is_ a nonsensical statement on its own.
Pavel Minaev
@Pavel: I agree. I should be "interpreted not compiled" which is the correct terminology.
0A0D
It's still clear what he means: A compiler would turn both notations into the same bytecode, while an interpreter has to go all the way on each run - therefore, [] means less to do for an interpreted language. Not that I think the microscopic amount of time saved will make any difference either way, but still.
Pekka
In javascript it's still an up front cost and not a runtime cost. Calling the difference microscopic would be an understatement.
Breton
+6  A: 

Elegance

To put it simply it just looks nicer AND is easier for a programmer to parse, especially for more complicated data structures:

var a= [
 ['a list', 'of', 'strings'],
 [1, 2, 3, 4],
 { hello: "This is an object property." }
];

Versus the clumsier OOP-oriented method:

var a= new Array();
var a2= new Array();
a2.push('a list');
a2.push('of');
a2.push('strings');
var a3= new Array();
a3.push(1);
a3.push(2);
a3.push(3);
a3.push(4);
var o= new Object();
o.hello= "This is an object property";
a.push(a2, a3, o);
pygorex1
Great answer... JSON notation is much MUCH more elegant than doing it programatically.
Zoidberg
+1 for notation
0A0D
Just to be pedantic - he's using object/array literals, not JSON. JSON is an extended concept designed for data interchange that has a slightly different set of rules (it's more constrained, and identifiers should be enclosed in quotes)
Luke Schafer
A: 

new Array(1, 2, 3) instead of [1, 2, 3] is the same as new String("cow") instead of "cow"

stereofrog
really depends on the runtime ... new String("cow") is boxed, and thus behaves like a normal object, whereas in some js implementations "cow" doesn't (you can assign dynamic properties to the 1st, but the 2nd only has the properties from it's prototype) ... same thing for Number and Boolean ...
back2dos
new String returns a new object of class String, this is a really important difference: strict equality will fail, the empty string->false conversion won't happen, you can attach properties to the object (you can't with a string).And i believe in all fast JS engines it will also end up being very slow to use.
olliej
+13  A: 

One good reason is because the Array constructor has completely non-intuitive behavior. For example:

var a = new Array(5);
console.log(a.length); //prints "5"
console.log(a[0]); //prints "undefined"

var b = new Array(1,2);
console.log(b.length); //prints "2"
console.log(b[0]); //prints "1"

In this case, a ends up being an array of size 5 with all elements undefined, and b ends up being an array of size 2, with the values [1,2].

var c = new Array("5");
console.log(c.length); //prints "1"
console.log(c[0]); //prints "5"

And here, you end up with a single-element array, containing "5"

In general, you should probably never use the constructors on built-in types in Javascript. They all have weird edge cases like this. For example:

var s = new String("Hello");
var l = "Hello";
console.log(typeof(s)); // prints "object"
console.log(typeof(l)); // prints "string"
Mark Bessey
+1 for the explanation on the non-intuitive behavior
0A0D
A: 

Less typing. That's it :)

OscarRyz