length 속성은 언제나 가장 큰 배열 요소의 인덱스보다 크며 unsigned, 32 비트 정수로 표현됩니다.
Property attributes of Array.prototype.length |
|
|---|---|
| Writable | yes |
| Enumerable | no |
| Configurable | no |
문법
arr.length
설명
length 속성의 값은 양의 정수이며 2의 32승 이하의 값을 가집니다. (232)
언제나 length 속성을 지정하여 배열을 절단할 수 있습니다. length 속성을 통해 배열을 늘리게 되면, 실질적 요소들의 개수는 증가하지 않습니다. 예를 들어 2의 length를 3으로 설정했다면, 배열은 여전히 단 2개의 요소만을 가집니다. 따라서, length 속성은 언제나 배열 내의 값들의 개수를 나타내지 않습니다. length와 수적인 속성의 관계에 대해 더 알아 보려면 해당 링크를 참고하세요.
예시
배열의 반복
다음 예시는, numbers 배열을 length 속성의 크기만큼 배열을 반복합니다. 각 요소 값은 두 배가 됩니다.
var numbers = [1, 2, 3, 4, 5];
for (var i = 0; i < numbers.length; i++) {
numbers[i] *= 2;
}
// numbers is now [2, 4, 6, 8, 10]
배열의 단축
다음 예시는 statesUS의 현재 length가 50이 넘을 경우 50의 length로 단축합니다.
if (statesUS.length > 50) {
statesUS.length = 50;
}
명세
| Specification | Status | Comment |
|---|---|---|
| ECMAScript 1st Edition (ECMA-262) | Standard | Initial definition. |
| ECMAScript 5.1 (ECMA-262) The definition of 'Array.length' in that specification. |
Standard | |
| ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Array.length' in that specification. |
Standard |
브라우저 호환성
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
| Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|
| Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |