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 = -1; // a.length == 6 (elements 0,1,2, and 5 defined) a = 0; // a.length == 50 (elements 0,1,2,5, and 49 defined)
The length property of a Navigator 3.0 array is not read-only. You can set length to a value smaller than its current value; the array will then be shortened to the new length--elements will be truncated from the end of the array, and their values will be lost. If you change the length property so that it is larger than its current value, the array will be made larger--new, undefined, elements will be added at the end to increase it to the newly specified size.
We've said that arrays are the same data type as objects are, and that any object can have array elements. This is true, but in Navigator 3.0, arrays created with the Array() constructor have features that other objects do not have. One of these features is the length property. If you create an object with the Object() constructor (or a constructor you define yourself) you can assign array elements to that object, but that object will not have the special length property described in this section.
But what is the point of the length property to begin with? One obvious feature is that it allows you to loop through the elements of an array:
sum = 0; for(var i = 0; i < arr.length; i++) sum += arr[i];
a = new Array(); a = -1; a = 0;
These lines of code define an array with two elements, one with index 5 and one with index 49. There are not any elements defined at indexes 0 through 4 and 6 through 48. An array like this with non-contiguous elements is sometimes called a "sparse" array. By contrast, an array with contiguous elements is sometimes called a "dense" array.