The length
property of an object which is an instance of type Array
sets or returns the number of elements in that array. The value is an unsigned, 32-bit integer that is always numerically greater than the highest index in the array.
var items = ['shoes', 'shirts', 'socks', 'sweaters']; items.length; // returns 4
Description
The value of the length
property is an integer with a positive sign and a value less than 2 to the 32nd power (232).
var namelistA = new Array(4294967296); //2 to the 32nd power = 4294967296 var namelistC = new Array(-100) //negative sign console.log(namelistA.length); //RangeError: Invalid array length console.log(namelistC.length); //RangeError: Invalid array length var namelistB = []; namelistB.length = Math.pow(2,32)-1; //set array length less than 2 to the 32nd power console.log(namelistB.length); //4294967295
You can set the length
property to truncate an array at any time. When you extend an array by changing its length
property, the number of actual elements does not increase; for example, if you set length
to 3 when it is currently 2, the array still contains only 2 elements. Thus, the length
property does not necessarily indicate the number of defined values in the array. See also Relationship between length
and numerical properties.
Property attributes of Array.length |
|
---|---|
Writable | yes |
Enumerable | no |
Configurable | no |
Writable
: If this attribute set tofalse
, the value of the property cannot be changed.Configurable
: If this attribute set tofalse
, any attempts to delete the property or change its attributes (Writable
,Configurable
, orEnumerable
) will fail.Enumerable
: If this attribute set totrue
, the property will be iterated over during for or for..in loops.
Examples
Iterating over an array
In the following example, the array numbers
is iterated through by looking at the length
property. The value in each element is then doubled.
var numbers = [1, 2, 3, 4, 5]; var length = numbers.length; for (var i = 0; i < length; i++) { numbers[i] *= 2; } // numbers is now [2, 4, 6, 8, 10]
Shortening an array
The following example shortens the array numbers
to a length of 3 if the current length is greater than 3.
var numbers = [1, 2, 3, 4, 5]; if (numbers.length > 3) { numbers.length = 3; } console.log(numbers); // [1, 2, 3] console.log(numbers.length); // 3
Specifications
Specification | Status | Comment |
---|---|---|
ECMAScript 1st Edition (ECMA-262) | Standard | Initial definition. |
ECMAScript 5.1 (ECMA-262) The definition of 'Array.length' in that specification. |
Standard | |
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Array.length' in that specification. |
Standard | |
ECMAScript Latest Draft (ECMA-262) The definition of 'Array.length' in that specification. |
Living Standard |
Browser compatibility
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
Feature | Android | Chrome for Android | Edge | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |