Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

Array: length

BaselineWidely available

Thelength data property of anArray instance represents 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.

Try it

const clothing = ["shoes", "shirts", "socks", "sweaters"];console.log(clothing.length);// Expected output: 4

Value

A nonnegative integer less than 232.

Property attributes ofArray: length
Writableyes
Enumerableno
Configurableno

Description

The value of thelength property is a nonnegative integer with a value less than 232.

js
const listA = [1, 2, 3];const listB = new Array(6);console.log(listA.length);// 3console.log(listB.length);// 6listB.length = 2 ** 32; // 4294967296// RangeError: Invalid array lengthconst listC = new Array(-100); // Negative numbers are not allowed// RangeError: Invalid array length

The array object observes thelength property, and automatically syncs thelength value with the array's content. This means:

  • Settinglength to a value smaller than the current length truncates the array — elements beyond the newlength are deleted.
  • Setting any array index (a nonnegative integer smaller than 232) beyond the currentlength extends the array — thelength property is increased to reflect the new highest index.
  • Settinglength to an invalid value (e.g., a negative number or a non-integer) throws aRangeError exception.

Whenlength is set to a bigger value than the current length, the array is extended by addingempty slots, not actualundefined values. Empty slots have some special interactions with array methods; seearray methods and empty slots.

js
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

See alsoRelationship betweenlength and numerical properties.

Examples

Iterating over an array

In the following example, the arraynumbers is iterated through by looking at thelength property. The value in each element is then doubled.

js
const numbers = [1, 2, 3, 4, 5];const length = numbers.length;for (let i = 0; i < length; i++) {  numbers[i] *= 2;}// numbers is now [2, 4, 6, 8, 10]

Shortening an array

The following example shortens the arraynumbers to a length of 3 if the current length is greater than 3.

js
const numbers = [1, 2, 3, 4, 5];if (numbers.length > 3) {  numbers.length = 3;}console.log(numbers); // [1, 2, 3]console.log(numbers.length); // 3console.log(numbers[3]); // undefined; the extra elements are deleted

Create empty array of fixed length

Settinglength to a value greater than the current length creates asparse array.

js
const numbers = [];numbers.length = 3;console.log(numbers); // [empty x 3]

Array with non-writable length

Thelength property is automatically updated by the array when elements are added beyond the current length. If thelength property is made non-writable, the array will not be able to update it. This causes an error instrict mode.

js
"use strict";const numbers = [1, 2, 3, 4, 5];Object.defineProperty(numbers, "length", { writable: false });numbers[5] = 6; // TypeError: Cannot assign to read only property 'length' of object '[object Array]'numbers.push(5); // // TypeError: Cannot assign to read only property 'length' of object '[object Array]'

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-properties-of-array-instances-length

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp