JavaScript Array Length Property
The JavaScript array's length property measures the number of currently available "spots" in the array, whether these spots have been initialized with a value or not. Array length is also a "read/write" property, which means that your scripts can not only retrieve it, but also modify it.
An array's length property returns an integer greater or equal to zero (<=0
); if the array is empty, the length returned is zero.
This tutorial covers the Array length property. Are you looking for the String length property?
Getting an Array's Length in JavaScript
The length property is dynamic, and is automatically updated by the JavaScript interpreter (built into your web browser) as the length changes.
// Array constructor without arguments (length=0)
var arlene = new Array();
Array length is always one more than the highest array index number, because the first element of a JavaScript array is zero. (See Referencing array elements.)
The last element of an array of length 6 is "array[5]
"
It is also possible to preset the array size (length) when declaring the array, by passing an integer to the new Array() constructor:
var arlene = new Array(7);
// We just declared an array of size 7 (length = 7)
alert( arlene.length.toString() );
Unsurprisingly, we get the following result:
Setting an Array's Length in JavaScript
In many other programming languages, setting the array size upon declaration is mandated. It is not the case with JavaScript, however, and it will increase your array size "on demand", as required by your scripts.
var arlene = new Array(7); // length = 7 (elements 0 through 6)
arlene[7] = "test 1";
arlene[8] = "test 2";
alert( arlene.length.toString() );
The script above demonstrates how the JavaScript interpreter will allocate more memory to your objects, if requested by your scripts.
Array Length is a Read/Write Property
Array length is a "read / write" property — your scripts can both retrieve it and set it. We saw above how to indirectly change an array length; our script will now explicitly assign a new length to an array.
// Declare an array of length 7
var arlene = new Array(7);
// Set its length to 9
arlene.length = 9;
// Get the current array size:
alert( arlene.length.toString() );
After declaring a size-7 array, our script sets the array length to 9. As expected, the alert returns an updated length:
Since the JavaScript interpreter takes care of expanding your array size as needed at runtime (during script execution), you will probably find yourself more often retrieving an array length than setting it.
The fact that JavaScript arrays use a zero-based index for their elements implies: (1) that the first element is Array[0]
, and (2) that Array[Array.length-1]
is the last array element (the index of the last element is one less than the number of elements).
Practically: the last element of an array of length 7 is Array[6]
, since the element count starts at zero…
Test the Array Length Property
Interactively test the array length property by editing the JavaScript code below and clicking the Test Array Length Property button.
Browser support for JavaScript array length property | |||
---|---|---|---|