start page | rating of books | rating of authors | reviews | copyrights

JavaScript: The Definitive Guide

Previous Chapter 8
Arrays
Next
 

8.5 Array Length Property

As we saw in the previous section, the Array() constructor method automatically initializes a length property for the array you create. When you create an array with this constructor (available only in Navigator 3.0 and later) this length property is automatically updated by JavaScript so that it is always one greater than the largest element number in the array. The following code illustrates this:

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[5] = -1;             // a.length == 6  (elements 0,1,2, and 5 defined)
a[49] = 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.

Because the Array() constructor and the array length property are not available in Navigator 2.0, JavaScript programs written for Navigator 2.0 often define custom array constructor functions that attempt to simulate the length property. (To avoid confusion with the "real" length property of arrays in 3.0, I prefer to name the property size in 2.0.) We saw such an array constructor in Example 8.1, and will learn more about arrays in Navigator 2.0 later in this chapter.

The length Property and Sparse Arrays

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];

This technique only works, of course, if the array in question has contiguous elements defined for each index between 0 and length-1. Since arrays in JavaScript are associative, sparse arrays, array elements do not have to be defined in contiguous blocks, like they do in C and related languages. For example, consider the code we saw above:

a = new Array();
a[5] = -1;
a[49] = 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.[2]

[2] Note though that the terms "sparse" and "dense" may also refer to the underlying implementation of the array, rather than to how you use it. JavaScript's arrays are implemented as sparse arrays, regardless of how you use them in any particular case.

When you are programming in JavaScript, you will typically use dense arrays with contiguous elements, if for no other reason than that you probably learned to program with languages that did not directly support sparse arrays.


Previous Home Next
Creating Arrays Book Index Array Methods

HTML: The Definitive Guide CGI Programming JavaScript: The Definitive Guide Programming Perl WebMaster in a Nutshell