[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/length [Back]  [Original]

Array: length - JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

Array: length

Baseline Widely available

This feature is well established and works across many devices and browser versions. Its been available across browsers since 2015 7.

Array length . 32 .

In this article

const clothing = ["shoes", "shirts", "socks", "sweaters"];

console.log(clothing.length);
// Expected output: 4

232 .

Property attributes of Array: length

length 232 .

js
const listA = [1, 2, 3];
const listB = new Array(6);

console.log(listA.length);
// 3

console.log(listB.length);
// 6

listB.length = 2 ** 32; // 4294967296
// RangeError: Invalid array length

const listC = new Array(-100); //    
// RangeError: Invalid array length

length , length . .

  • length . length .
  • (232 , ) length . length .
  • length (: ) RangeError .

length undefined . . .

js
const arr = [1, 2];
console.log(arr);
// [ 1, 2 ]

arr.length = 5; //   2   5 .
console.log(arr);
// [ 1, 2, <3 empty items> ]

arr.forEach((element) => console.log(element));
// 1
// 2

length .

numbers length . .

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]

numbers 3 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); // 3
console.log(numbers[3]); // undefined;   .

length .

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

length

length Array . length Array . .

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]'

Specification
ECMAScript 2027 LanguageSpecification
# sec-properties-of-array-instances-length


Web Proxy Viewer  |  New URL  |  Original Page