74

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
17.1k4 gold badges57 silver badges78 bronze badges
asked Jul 7, 2009 at 20:36
3
  • 1
    This is similar to, but not quite the same as: stackoverflow.com/questions/931872/… Commented Jul 7, 2009 at 22:41
  • 1
    duplicate of What's wrong with var x = new Array(); Commented Jun 27, 2012 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 Commented Dec 20, 2017 at 14:59

4 Answers 4

109

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?

answered Jul 7, 2009 at 20:38
4
  • 21
    It is the "new" way...no pun intended? Commented Mar 25, 2012 at 17:40
  • 3
    But the answer doesn't explain that when we should use literal i.e.[] and when to use new Array(); Commented Oct 8, 2013 at 10:53
  • 5
    Always use the literal []. The reason this is better is that it's safer because someone could potentially overwrite the window.Array constructor but not the literal. Commented Nov 7, 2014 at 19:12
  • 1
    For those using TypeScript, the equivalent is var a: string[] = [];. Commented Oct 18, 2019 at 3:19
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.

answered Jul 7, 2009 at 23:19
0
2

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

Useless Code
12.5k5 gold badges37 silver badges42 bronze badges
answered Jan 4, 2014 at 6:29
-1

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 [].

answered Apr 25, 2014 at 14:22

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.