contents   index   previous   next

Array length




The length property returns one more than the largest index of the array. Note that this value does not necessarily represent the actual number of elements in an array, since elements do not have to be contiguous.


By changing the value of the length property, you can remove array elements. For example, if you change ant.length to 2, ant will only have the first two members, and the values stored at the other indices will be lost. If we set bee.length to 2, then bee will consist of two members: bee[0], with a value of 88, and bee[1], with an undefined value.



Array(), global.getArrayLength(), global.setArrayLength()



// Suppose we had two arrays "ant" and "bee",

// with the following elements:


var ant = new Array();

ant[0] = 3;

ant[1] = 4;

ant[2] = 5;

ant[3] = 6;


var bee = new Array();

bee[0] = 88;

bee[3] = 99;


// The length property of both ant and bee

// is equal to 4, even though ant has twice

// as many actual elements as bee does.


Array object instance methods