Share via


length Property (Array)

Returns an integer value one higher than the highest element defined in an array.

arrayObj.length 

Arguments

  • arrayObj
    Required. Any Array object.

Remarks

As the elements in a JScript array do not have to be contiguous, the length property is not necessarily the number of elements in the array.

If a value smaller than its previous value is assigned to the length property, the array is truncated, and any elements with array indexes equal to or greater than the new value of the length property are lost.

If a value larger than its previous value is assigned to the length property, the array is formally expanded, but no new elements are created.

Example

The following example illustrates the use of the length property.

var s = "";
var arr = new Array(10, 11, "abc", "def");
for (var i = 0; i < arr.length; i++)
    {
    s += arr[i] + " ";
    }
// Output: 10 11 abc def

In the following example, an array is declared, and two elements are added to it. Since the largest index in the array is 6, the length is 7.

var my_array = new Array();
my_array[2] = "Test";
my_array[6] = "Another Test";

var s = my_array.length;
// Output: 7

Requirements

Version 2

Applies To:

Array Object

See Also

Reference

length Property (Function)

length Property (String)