| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/Array/length | [Back] [Original] |
Get to know MDN better
This feature is well established and works across many devices and browser versions. Its been available across browsers since 20157.
length Array 32
var items = ["shoes", "shirts", "socks", "sweaters"];
items.length;
// returns 4
length 0 2^32 ().
var namelistA = new Array(4294967296); //2^32 = 4294967296
var namelistC = new Array(-100); //
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; // 0 ~ 2^32 -1
console.log(namelistB.length);
//4294967295
length length array.length 2 3 3 (non-iterable)(empty slot)
const arr = [1, 2]; console.log(arr); // [ 1, 2 ] arr.length = 5; // arrlength25 console.log(arr); // [ 1, 2, <3 empty items> ] arr.forEach(element => console.log(element)); // // 1 // 2
length length
Array.length | |
|---|---|
Writable: falseConfigurable: false(Writable, Configurable, or Enumerable)Enumerable: true for for..in , numbers length
var numbers = [1, 2, 3, 4, 5];
var length = numbers.length;
for (var i = 0; i < length; i++) {
numbers[i] *= 2;
}
// numbers [2, 4, 6, 8, 10]
, numbers 3 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
| Specification |
|---|
| ECMAScript 2027 LanguageSpecification # sec-properties-of-array-instances-length |
This page was last modified on 2025714 by MDN contributors.
Arrayat()Array.prototype.concat()Array.prototype.copyWithin()Array.prototype.entries()every()Array.prototype.fill()Array.prototype.filter()Array.prototype.find()Array.prototype.findIndex()findLast()findLastIndex()Array.prototype.flat()flatMap()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()reduceRight()Array.prototype.reverse()Array.prototype.shift()Array.prototype.slice()some()Array.prototype.sort()Array.prototype.splice()toLocaleString()toReversed()toSorted()toSpliced()Array.prototype.toString()Array.prototype.unshift()Array.prototype.values()with()Array.prototype[Symbol.iterator]()Object/FunctionYour blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |