What's wrong with var x = new Array();

Chris S picture Chris S · May 19, 2009 · Viewed 18.3k times · Source

In JSLint, it warns that

var x = new Array();

(That's not a real variable name) should be

var result = [];

What is wrong with the 1st syntax? What's the reasoning behind the suggestion?

Answer

Dan Lew picture Dan Lew · May 19, 2009

It's safer to use [] than it is to use new Array(), because you can actually override the value of Array in JavaScript:

Array = function() { };

var x = new Array();
// x is now an Object instead of an Array.

In other words, [] is unambiguous.