Array literal (Declaration)

A means of creating and initializing an array at once.

Availability:

ECMAScript edition - 3
JavaScript - 1.3
JScript - 5.0
Internet Explorer - 5.0
Netscape - 4.7
Property/method value type:Array object
JavaScript syntax:-[ anElement, ... ]
Argument list:anElementAn element to be stored in the array

JavaScript version 1.2 introduces the capability of assigning values to an array as it is created and building the array without first using a constructor.

Now array construction can also be nested to create multi-dimensional arrays.

The result is an array containing the elements defined by the literal expression.

Warnings:

Example code:

   <SCRIPT>

   // Create a simple array literal

   var myArray = [ 100, 1.34, "String text", true, { prop:100 } ];

   // Create a nested multi-dimensional array

   var matarray = [ [1,0], [0,1]];

   // JavaScript expression in arrays

   var exprarray = [ Math.random()*10, Math.random()*100 ];

   // Sparse array

   var sparse = [100, , , , , 1000];

   </SCRIPT>

See also:Array object

Cross-references:

ECMA 262 edition 3 - section - 11.1.4

O'Reilly JavaScript Definitive Guide - page - 46