Array: length
Baseline
Widely available
This feature is well established and works across many devices and browser versions. Itâs been available across browsers since â¨2015ë 7ìâ©.
Array ì¸ì¤í´ì¤ì length ë°ì´í° ìì±ì í´ë¹ ë°°ì´ì ìì ì를 ëíë
ëë¤. í´ë¹ ê°ì ë¶í¸ ìë 32ë¹í¸ ì ìì´ë©° ë°°ì´ì ìµë ì¸ë±ì¤ ë³´ë¤ íì í½ëë¤.
ìëí´ ë³´ê¸°
const clothing = ["shoes", "shirts", "socks", "sweaters"];
console.log(clothing.length);
// Expected output: 4
ê°
232ë³´ë¤ ìì ììê° ìë ì ì.
Property attributes of Array: length | |
|---|---|
| ì°ê¸° ê°ë¥ | ê°ë¥ |
| ì´ê±° ê°ë¥ | ë¶ê°ë¥ |
| ì¤ì ê°ë¥ | ë¶ê°ë¥ |
ì¤ëª
length ìì±ì ê°ì ìì ì ìì´ë©° 232 미ë§ì ê°ì ê°ì§ëë¤.
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 ê°ì´ ìë ë¹ ì¬ë¡¯ì ì¶ê°íì¬ ë°°ì´ì íì¥í©ëë¤. ë¹ ì¬ë¡¯ì ë°°ì´ ë°©ë²ê³¼ ëª ê°ì§ í¹ë³í ìí¸ ìì©ì í©ëë¤. ë°°ì´ ë°©ë²ê³¼ ë¹ ì¬ë¡¯ì 참조íììì¤.
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 ìì±ì í¬ê¸°ë§í¼ ìíí©ëë¤. ê° ììì ê°ì ë ë°°ê° ë©ëë¤.
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ì¼ë¡ ì¤ì
ëë¤.
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를 íì¬ í¬ê¸°ë³´ë¤ ë í¬ê² ì¤ì íë©´ í¬ì ë°°ì´ì ìì±í©ëë¤.
const numbers = [];
numbers.length = 3;
console.log(numbers); // [empty x 3]
length ì°ê¸° ë¶ê°ë¥í ë°°ì´
length ìì±ì íì¬ ê¸¸ì´ë¥¼ ëì´ ììê° ì¶ê°ëë©´ Arrayì ìí´ ìëì¼ë¡ ì
ë°ì´í¸ë©ëë¤. length ìì±ì´ ì°ê¸° ë¶ê°ë¥í ê²½ì° Arrayë ì´ë¥¼ ì
ë°ì´í¸í ì ììµëë¤. ì´ë¡ ì¸í´ ì격 모ëìì ì¤ë¥ê° ë°ìí©ëë¤.
"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® 2026 Language Specification > # sec-properties-of-array-instances-length > |