Condense code with JavaScript's literal array notation

Chances are, you probably create JavaScript arrays with the new Array() keyword. However, under certain circumstances JavaScript provides an even easier way to create an array. When you create an array for which your code supplies the literal values, you can simply surround the list in brackets ([ ]). For instance, suppose your code created an array like so:

jams = new Array("grape", "cherry", "peach");

Instead, you can use the condensed notation:

jams = ["grape", "cherry", "peach"];

When JavaScript comes across this notation, it automatically builds a zero-indexed array of these items.

To create a multidimensional array, simply nest the literal notation lists within another, as in
cavemen = [["Fred", "Flintstone"], ["Wilma", "Flintstone"], ["Barney", "Rubble"]]

Source: Mike D. Jones
Viewed 4839 times