7.6.3. Array Length
All arrays, whether created with the Array()
constructor or defined with
an array literal, have a special length
property that specifies how many
elements the array contains. More precisely, since arrays can have undefined
elements, the length
property is always one larger than the largest
element number in the array. Unlike regular object properties, the length
property of an array is automatically updated to maintain this invariant when
new elements are added to the array. The following code illustrates:
var a = new Array(); // a.length == 0 (no elements defined)
a = new Array(10); // a.length == 10 (empty elements 0-9 defined)
a = new Array(1,2,3); // a.length == 3 (elements 0-2 defined)
a = [4, 5]; // a.length == 2 (elements 0 and 1 defined)
a[5] = -1; // a.length == 6 (elements 0, 1, and 5 defined)
a[49] = 0; // a.length == 50 (elements 0, 1, 5, and 49 defined)
Remember that array indexes must be less than 2 32 -1, which means that the
largest possible value for the length
property is 2 32 -1.