views:

1852

answers:

2

JSLint is giving me this error:

Problem at line 11 character 33: Use the array literal notation [].

var myArray = new Array();

What is array literal notation and why does it want me to use it instead?

It shows here that new Array(); should work fine... is there something I'm missing?

+7  A: 

array literal notation is where you define a new array using just empty brackets. In your example:

var myArray = [];

It is the "new" way of defining arrays, and I suppose it is shorter/cleaner.

CookieOfFortune
A: 

See also: What’s wrong with var x = new Array();

Aside from the Crockford argument, I believe it is also due to the fact that other languages have similar data structures that happen to use the same syntax; for example, Python has lists and dictionaries; see the following examples:

// this is a Python list
a = [66.25, 333, 333, 1, 1234.5]

// this is a Python dictionary
tel = {'jack': 4098, 'sape': 4139}

Isn't it neat how Python is also grammatically correct Javascript? (yes, the ending semi-colons are missing, but those aren't required for Javascript, either)

Thus, by reusing common paradigms in programming, we save everyone from having to relearn something that shouldn't have to.

ken