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.
The value of the
length
property is an integer with a positive sign and a value less than 2 to the 32nd power (2
32
).
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 increases; for example, if you set
length
to 3 when it is currently 2, the array now contains 3 elements, which causes the third element to be a non-iterable empty slot.
const arr = [1, 2]; console.log(arr); // [ 1, 2 ] arr.length = 5; // set array length to 5 while currently 2. console.log(arr); // [ 1, 2, <3 empty items> ] arr.forEach(element => console.log(element)); // 1 // 2
As you can see, the
length
property does not necessarily indicate the number of defined values in the array. See also
Relationship between
length
and numerical properties
.
特性属性在
Array.prototype.length
|
|
|---|---|
| 可写 | yes |
| 可枚举 | no |
| 可配置 | no |
可写
: If this attribute set to
false
, the value of the property cannot be changed.
可配置
: If this attribute set to
false
, any attempts to delete the property or change its attributes (
可写
,
可配置
,或
可枚举
) will fail.
可枚举
: If this attribute set to
true
, the property will be iterated over during
for
or
for..in
loops.
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]
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
var numbers = []; numbers.length = 3; console.log(numbers); // [undefined, undefined, undefined]
| 规范 |
|---|
|
ECMAScript (ECMA-262)
The definition of 'Array.length' in that specification. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
length
|
Chrome 1 | Edge 12 | Firefox 1 | IE 4 | Opera 4 | Safari 1 | WebView Android ≤37 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs 0.1.100 |
完整支持
Array
Array.prototype.length
Array.prototype[@@unscopables]
Array.from()
Array.isArray()
Array.of()
Array.prototype.concat()
Array.prototype.copyWithin()
Array.prototype.entries()
Array.prototype.every()
Array.prototype.fill()
Array.prototype.filter()
Array.prototype.find()
Array.prototype.findIndex()
Array.prototype.flat()
Array.prototype.flatMap()
Array.prototype.forEach()
Array.prototype.includes()
Array.prototype.indexOf()
Array.prototype.join()
Array.prototype.keys()
Array.prototype.lastIndexOf()
Array.prototype.map()
Array.prototype.pop()
Array.prototype.push()
Array.prototype.reduce()
Array.prototype.reduceRight()
Array.prototype.reverse()
Array.prototype.shift()
Array.prototype.slice()
Array.prototype.some()
Array.prototype.sort()
Array.prototype.splice()
Array.prototype.toLocaleString()
Array.prototype.toSource()
Array.prototype.toString()
Array.prototype.unshift()
Array.prototype.values()
Array.prototype[@@iterator]()
get Array[@@species]
Function
Object
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.setPrototypeOf()