Availability: |
| ||||||||
Property/method value type: | Depends on array content | ||||||||
JavaScript syntax: | - | myArray[anIndex] | |||||||
Argument list: | anIndex | A 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.
Be aware that your script is referring to array elements starting at zero. You can get subtle 'off-by-one' errors if you assume that the array begins at item 1.
In Netscape 2.02, the length property of an array cannot be relied on to hold the right value.
You should avoid putting spaces into associative names because it introduces a property whose name cannot be reached other than via an array index. Not all implementations will trap this error situation. A property name is an identifier and identifier names cannot contain spaces so it should throw an exception.
<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>
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
Prev | Home | Next |
Arithmetic type | Up | Array literal |
JavaScript Programmer's Reference, Cliff Wootton Wrox Press (www.wrox.com) Join the Wrox JavaScript forum at p2p.wrox.com Please report problems to support@wrox.com © 2001 Wrox Press. All Rights Reserved. Terms and conditions. |