Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
21
rated 0 times [  26] [ 5]  / answers: 1 / hits: 78390  / 15 Years ago, tue, july 7, 2009, 12:00:00

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?


More From » arrays

 Answers
34

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?



[#99172] Thursday, July 2, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
austonjuancarlosb

Total Points: 238
Total Questions: 89
Total Answers: 99

Location: Chad
Member since Mon, Dec 5, 2022
1 Year ago
;