71

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?

Purag
  • 16,273
  • 4
  • 48
  • 70
Matt
  • 5,207
  • 23
  • 73
  • 111
  • 1
    This is similar to, but not quite the same as: http://stackoverflow.com/questions/931872/whats-the-difference-between-array-and-while-declaring-a-javascript-arr/ – Borgar Jul 07 '09 at 22:41
  • 1
    duplicate of [What's wrong with var x = new Array();](http://stackoverflow.com/questions/885156/whats-wrong-with-var-x-new-array) – Bergi Jun 27 '12 at 18:07
  • 1
    Also always use the literal when you can because the Array constructor ( new Array() ) doesn’t always work properly. e.g. if there is a single value that is a number. > new Array(3, 11, 8) [ 3, 11, 8 ] > new Array(3) [ , , ,] > new Array(3.1) RangeError: Invalid array length – Ingadi Dec 20 '17 at 14:59

4 Answers4

102

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.

The examples below explain the difference between them:

var a = [],            // these are the same
    b = new Array(),   // a and b are arrays with length 0

    c = ['foo', 'bar'],           // these are the same
    d = new Array('foo', 'bar'),  // c and d are arrays with 2 strings

    // these are different:
    e = [3],             // e.length == 1, e[0] == 3
    f = new Array(3);   // f.length == 3, f[0] == undefined

Reference: What’s the difference between “Array()” and “[]” while declaring a JavaScript array?

Community
  • 1
  • 1
CookieOfFortune
  • 12,980
  • 8
  • 36
  • 56
23

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.

Community
  • 1
  • 1
ken
  • 3,413
  • 1
  • 25
  • 37
3

Aside from the Crockford argument, jsPerf says that it's faster. http://jsperf.com/new-vs-literal-array-declaration

Useless Code
  • 10,586
  • 4
  • 30
  • 37
ecMode
  • 492
  • 3
  • 15
0

After looking at @ecMode jsperf, I did some further tests.

When using push to add to the array new Array() is considerably faster on Chrome:

http://jsperf.com/new-vs-literal-array-declaration/2

Using index to add is slightly faster for [].

Skeep
  • 2,741
  • 1
  • 17
  • 20