This page was translated from English by the community.Learn more and join the MDN Web Docs community.
Array.prototype[Symbol.iterator]()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2016년 9월.
Array 인스턴스의[Symbol.iterator]() 메서드는순회 가능 프로토콜을 구현하며, 배열을전개 구문이나for...of 루프와 같이 순회 가능을 기대하는 대부분의 구문에서 사용할 수 있도록 합니다. 이 메서드는 배열의 각 인덱스 값을 산출하는배열 순회자 객체를 반환합니다.
이 속성의 초기 값은Array.prototype.values 속성의 초기 값과 동일한 함수 객체입니다.
In this article
시도해 보기
const array1 = ["a", "b", "c"];const iterator1 = array1[Symbol.iterator]();for (const value of iterator1) { console.log(value);}// Expected output: "a"// Expected output: "b"// Expected output: "c"구문
array[Symbol.iterator]()매개변수
없음.
반환 값
Array.prototype.values()와 동일한 반환 값입니다. 이는 배열의 각 인덱스의 값을 산출하는 새로운순회 가능 순회자 객체입니다.
예제
>for...of 루프를 사용한 순회
이 메서드를 직접 호출할 필요는 거의 없습니다.[Symbol.iterator] 메서드가 있으면 배열을순회 가능으로 만들 수 있으며,for...of 루프와 같은 순회 구문은 이 메서드를 자동으로 호출하여 반복할 순회자를 얻습니다.
HTML
<ul></ul>JavaScript
const arr = ["a", "b", "c"];const letterResult = document.getElementById("letterResult");for (const letter of arr) { const li = document.createElement("li"); li.textContent = letter; letterResult.appendChild(li);}결과
순회자를 수동으로 부르기
반환된 순회자 객체의next 메서드를 수동으로 호출하여 순회 과정을 최대한 제어할 수 있습니다.
const arr = ["a", "b", "c", "d", "e"];const arrIter = arr[Symbol.iterator]();console.log(arrIter.next().value); // aconsole.log(arrIter.next().value); // bconsole.log(arrIter.next().value); // cconsole.log(arrIter.next().value); // dconsole.log(arrIter.next().value); // e동일한 함수로 문자열과 문자열 배열 처리하기
문자열과 배열은 모두 순회 가능 프로토콜을 구현하기 때문에, 범용 함수는 두 입력을 동일한 방식으로 처리하도록 설계할 수 있습니다. 이는 입력이 배열이거나 적어도 해당 메서드가 있는 객체여야 하는Array.prototype.values()를 직접 호출하는 것보다 낫습니다.
function logIterable(it) { if (typeof it[Symbol.iterator] !== "function") { console.log(it, "is not iterable."); return; } for (const letter of it) { console.log(letter); }}// ArraylogIterable(["a", "b", "c"]);// a// b// c// StringlogIterable("abc");// a// b// c// NumberlogIterable(123);// 123은 순회 가능이 아닙니다.명세서
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-array.prototype-%symbol.iterator%> |