Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In JavaScript, why is [ ] preferred over new Array();?

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?

Here is one reason I can think of on why [] is better than new Array();:

var Array = function () { };

Overriding the Array object will break code...!

Any more reasons?

like image 975
Andreas Grech Avatar asked Nov 25 '09 23:11

Andreas Grech


People also ask

What is the difference between array and new array?

When Array is called as a function rather than as a constructor, it creates and initialises a new Array object. Thus the function call Array(…) is equivalent to the object creation expression new Array(…) with the same arguments.

Why use a Set instead of an array?

A set stores distinct values of the same type in a collection with no defined ordering. You can use a set instead of an array when the order of items is not important, or when you need to ensure that an item only appears once.

What is the main benefit of the array keyword in JS?

The Array object lets you store multiple values in a single variable. It stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.

What is the difference between Set and array JavaScript?

The Set data type was introduced in ES2015 and the difference between array and set is that while an array can have duplicate values a set can't. Elements can be accessed in array using index which isn't possible in Set since it uses keys and elements can be traversed only in the way they were entered.


2 Answers

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);
like image 175
barkmadley Avatar answered Oct 14 '22 22:10

barkmadley


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"
like image 28
Mark Bessey Avatar answered Oct 14 '22 22:10

Mark Bessey