please dont rip this site

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

file: /Techref/language/java/script/definitive/ch08_05.htm, 8KB, , updated: 2019/10/14 15:00, local time: 2024/11/25 11:29,
TOP NEW HELP FIND: 
3.135.216.196:LOG IN

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE!

<A HREF="http://ecomorder.com/techref/language/java/script/definitive/ch08_05.htm"> [Chapter 8] 8.5 Array Length Property</A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.


Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?

 

Welcome to ecomorder.com!

 

Welcome to ecomorder.com!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  .