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.
Netscape 4 does not mind an extra trailing comma (as per the C language convention). To force an undefined element to be assigned to the end of the array, you must place two trailing commas.
MSIE adds an undefined element for each trailing comma. This means that MSIE creates arrays that are one item longer than Netscape does if there is a trailing comma.
Some revisions of Netscape exhibit a further problem in that a single numeric value in the square brackets is interpreted as an array length value. This is consistent with the Array() constructor but is not correct in this context. You can place a pair of trailing commas there to fix this at the expense of some wasted array items that contain undefined values. This is not extant on all versions and may be encountered only rarely now.
<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 |
ECMA 262 edition 3 - section - 11.1.4
O'Reilly JavaScript Definitive Guide - page - 46
Prev | Home | Next |
Array index delimiter ([ ]) | Up | Array object |
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. |