Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is array literal notation in javascript and when should you use it?

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?

like image 857
Matt Avatar asked Jul 07 '09 20:07

Matt


People also ask

What is an array literal notation of JavaScript?

In Javascript, an array literal is a list of expressions, each of which represents an array element, enclosed in a pair of square brackets ' [ ] ' . When an array is created using an array literal, it is initialized with the specified values as its elements, and its length is set to the number of arguments specified.

When would you use an array in JavaScript?

In JavaScript, array is a single variable that is used to store different elements. It is often used when we want to store list of elements and access them by a single variable.

How can we create an array in JavaScript using array literal?

Creating an Array Using an array literal is the easiest way to create a JavaScript Array. Syntax: const array_name = [item1, item2, ...]; It is a common practice to declare arrays with the const keyword.

Which of the following is a correct JavaScript array literal?

The correct way to write a JavaScript array var txt = new Array("arr ","kim","jim"). JavaScript arrays are used to store multiple values in a single variable.


2 Answers

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?

like image 123
CookieOfFortune Avatar answered Sep 30 '22 23:09

CookieOfFortune


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.

like image 31
ken Avatar answered Sep 30 '22 23:09

ken