Array index delimiter ([ ]) (Delimiter)

Access elements of an array with this delimiter.

Availability:

ECMAScript edition - 2
JavaScript - 1.1
JScript - 3.0
Internet Explorer - 4.0
Netscape - 3.0
Netscape Enterprise Server - 2.0
Opera - 3.0
Property/method value type:Depends on array content
JavaScript syntax:-myArray[anIndex]
Argument list:anIndexA legal index value into the array, not greater than the array length

Array elements are indexed by selecting them numerically within the set of elements contained in the array. The length property of an array indicates how many indexable locations there are. Array elements begin with the zeroth item.

Storing values into indexes that are higher than the current value of the length property will automatically extend the array and reset the length property. An array with only one entry in the 100th element (index value 99) is very sparsely populated but still should report a length value of 100.

In Netscape, referencing the array with no element delimiters will yield a comma-separated list of the contents of the array. So this:

   myArray = new Array(6);
   myArray[0] = 0;
   myArray[1] = "XXX";
   myArray[2] = 0;
   myArray[3] = "XXX";
   myArray[4] = 0;
   myArray[5] = "XXX";
   document.write(myArray);

Yields this when executed:

0,XXX,0,XXX,0,XXX

Accessing properties of an object by name simply requires the name to be added to the object reference with a dot separator between them. Numeric values cannot be used in this way. You must use a string to name the Array element when it is assigned.

The associativity is left to right.

Refer to the operator precedence topic for details of execution order.

Although JavaScript does not properly support multi-dimensional arrays, you can simulate them by storing references to one array in the elements of another. You need to create a separate array for each row and then one master array to arrange them into a column.

True multi-dimensional arrays would use a notation like this:

multiArray[1,2]

But in JavaScript we can at least manage this:

multiArray[1][2]

This is close enough that most programmers will be able to cope with it quite happily.

Another alternative way to do this is to use a single dimensional array, but calculate the indices. For example to make a 5 x 5 array, you would create a single dimensional array that is 25 elements long. Then to reach the rows you use the row number and multiply the value by 5 before adding the column number to access the desired cell. You need to be careful though because if you have an 'off-by-one' error, it all goes wrong.

Warnings:

Example code:

   <SCRIPT>
   // Multidimensional array simulation
   hExtent = 5;
   vExtent = 6;
   theExtent = hExtent * vExtent;
   myArray = new Array(theExtent);
   document.write("<TABLE BORDER=1>");
   for(vEnum = 0; vEnum < vExtent; vEnum++)
   {
      document.write("<TR>");
      for(hEnum = 0; hEnum < hExtent; hEnum++)
      {
         targetCell = (vEnum * hExtent) + hEnum;
         document.write("<TD>");
         document.write(vEnum);
         document.write(",");
         document.write(hEnum);
         document.write(" = ");
         document.write(targetCell);
         document.write("</TD>");
      }
      document.write("</TR>");
   }
   document.write("</TABLE>");
   </SCRIPT>

See also:Array object, Array.length, Associativity, Multi-dimensional arrays, Off by one errors, Operator Precedence, Postfix operator, Property name

Cross-references:

ECMA 262 edition 2 - section - 7.6

ECMA 262 edition 2 - section - 11.2

ECMA 262 edition 3 - section - 7.7

Wrox Instant JavaScript - page - 16

Wrox Instant JavaScript - page - 32

Wrox Instant JavaScript - page - 33