Export (0) Print
Expand All

length Property (Array) (JavaScript)

Gets or sets the length of the array. This is a number one higher than the highest element defined in an array.

numVar = arrayObj.length 

numVar

Required. Any number.

arrayObj

Required. Any Array object.

In JavaScript arrays are sparse, and the elements in an array do not have to be contiguous. The length property is not necessarily the number of elements in the array. For example, in the following array definition, my_array.length contains 7, not 2:

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

If you make the length property smaller than its previous value, 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 you make the length property larger than its previous value, the array is expanded, and any new elements created have the value undefined.

The following example illustrates the use of the length property:

var a;
a = new Array(0,1,2,3,4);
document.write(a.length);

// Output
// 5

Supported in the following document modes: Quirks, Internet Explorer 6 standards, Internet Explorer 7 standards, Internet Explorer 8 standards, Internet Explorer 9 standards, Internet Explorer 10 standards, Internet Explorer 11 standards. Also supported Store apps (Windows 8 and Windows Phone 8.1). See Version Information.

Note Note

Starting with Internet Explorer 9 Standards mode, trailing commas included in the initialization of an Array are handled differently.

Show:
© 2015 Microsoft