Array.length Property

ActionScript for Flash MX: The Definitive Guide, 2nd Edition
By Colin Moock
Chapter 18.  ActionScript Language Reference
Array.length Property Flash 5

the number of elements in an array read/write
array.length

Description

The length property is a nonnegative integer specifying the number of elements in an array. An array with no elements has a length of 0; an array with 2 elements has a length of 2. Note that the index number of the first element in an array is 0, so length is always one greater than the index of the last element in the array.

The length property of an array indicates how many numbered elements the array currently contains, including empty elements (those containing null or undefined). For example, an array may have values for elements 0, 1, 2, and 9, but elements 3 through 8 may be empty. Such an array has a length of 10, because it has 10 element positions (0 through 9), even though only 4 positions are occupied by useful values.

The length property changes automatically whenever any elements are added or removed via the Array class methods, such as push( ). The length property reflects numbered elements only; it does not include named array elements, which are treated as properties of the Array object. Although we ordinarily use Array class methods to change length indirectly, length can also be manipulated directly. Setting the length of an array changes the number of elements in the array. If we increase length, empty elements are added to the end of the array and assigned the value undefined. If we decrease length, existing elements are removed from the end of the array without warning; therefore splice( ) or pop( ) are the preferred methods of deleting elements from an array.

Example

myList = new Array("one", "two", "three"); trace(myList.length);  // Displays: 3     // Loop through the array's elements for (var i = 0; i < myList.length; i++) {   trace(myList[i]); }

See Also

Section 11.5

     



    ActionScript for Flash MX. The Definitive Guide
    ActionScript for Flash MX: The Definitive Guide, Second Edition
    ISBN: 059600396X
    EAN: 2147483647
    Year: 2002
    Pages: 780
    Authors: Colin Moock

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net