Files
2nd/10_Wiki/Topic_JavaScript/JavaScript_Iterables.md
T
koriweb 9609c04755 docs(10_Wiki): W3Schools 위키화 — HTML/CSS/JavaScript(core)
W3Schools 튜토리얼을 P-Reinforce v3.1 포맷으로 위키화(영어 본문, 한/영 섹션 헤더).
- Topic_HTML: 59문서 (튜토리얼+예제, 레퍼런스/메타 제외)
- Topic_CSS: 190문서 (메인 + Advanced/Flexbox/Grid/RWD 전체)
- Topic_JavaScript: 120문서 (코어 언어; Temporal/DOM상세/BOM/WebAPI/AJAX/jQuery/Graphics 등은 후속)
각 폴더 00_INDEX.md(MOC) 포함. 코드 verbatim, 미확인분은 "Not found in source" 표기.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-23 19:21:18 +09:00

146 lines
5.8 KiB
Markdown

---
id: javascript-iterables
title: "JavaScript Iterables"
category: "Frontend"
status: "draft"
verification_status: "conceptual"
canonical_id: ""
aliases: ["JS iterables", "iterable protocol", "for...of", "Symbol.iterator", "iterable objects"]
duplicate_of: ""
source_trust_level: "B"
confidence_score: 0.88
created_at: 2026-06-23
updated_at: 2026-06-23
review_reason: ""
merge_history: []
tags: ["javascript", "js", "web", "frontend", "w3schools", "iterables", "for-of", "symbol-iterator"]
raw_sources: ["https://www.w3schools.com/js/js_iterables.asp"]
applied_in: []
github_commit: ""
---
# [[JavaScript Iterables]]
## 🎯 한 줄 통찰 (One-line insight)
Iterables are objects that can be looped over with the `for...of` statement; in JavaScript, Strings, Arrays, Typed Arrays, Sets, and Maps are all iterable because their prototypes carry a `Symbol.iterator` method. [S1]
## 🧠 핵심 개념 (Core concepts)
- **Iterable = loopable with `for...of`** — an iterable is an object that can be iterated (looped) over with the `for...of` statement. [S1]
- **Built-in iterables** — in JavaScript the following are iterables: Strings, Arrays, Typed Arrays, Sets, and Maps, because their prototypes have a `Symbol.iterator` method. [S1]
- **The iterator protocol** — an object becomes an iterator by implementing a `next()` method that returns an object with `value` (the next value) and `done` (a boolean indicating completion). [S1]
- **`Symbol.iterator` is the hook** — an object is made iterable by defining a `Symbol.iterator` method that returns an iterator. [S1]
## 🧩 추출된 패턴 (Extracted patterns)
- **`for...of` over any iterable** — the same `for (variable of iterable)` loop works uniformly across strings, arrays, sets, and maps. [S1]
- **Custom iterable via `Symbol.iterator`** — attach a `Symbol.iterator` function to a plain object so it can be used in `for...of`, giving you custom iteration logic for your own data structures. [S1]
## 📖 세부 내용 (Details)
**Iterables**
Iterables are iterable objects (like Arrays). Iterables can be accessed with simple and efficient code. Iterables can be iterated over with `for...of` loops. [S1]
**The `for...of` Loop**
The `for...of` statement loops through the elements of an iterable object. Its syntax is: [S1]
```javascript
for (variable of iterable) {
// code block to be executed
}
```
**Iterating over a String**
You can use a `for...of` loop to iterate over the elements of a string: [S1]
```javascript
const name = "W3Schools";
for (const x of name) {
// code block to be executed
}
```
**Iterating over an Array**
You can use a `for...of` loop to iterate over the elements of an array: [S1]
```javascript
const letters = ["a","b","c"];
for (const x of letters) {
// code block to be executed
}
```
**Iterating over Sets and Maps**
`for...of` can also iterate over the elements of a Set and over the key/value pairs of a Map. [S1]
**JavaScript Iterators**
The `for...of` loop relies on an iterator. The iterable must implement a method named `Symbol.iterator`. The `Symbol.iterator` method is called automatically by `for...of`. It returns an iterator object with a `next()` method. The `next()` method returns an object with a `value` property (the next value) and a `done` property (`true` or `false`). [S1]
**Built-in Iterables**
In JavaScript the following are iterables: Strings, Arrays, Typed Arrays, Sets, and Maps. They are iterable because their prototype objects all have a `Symbol.iterator` method. [S1]
**A User-Defined (Home-Made) Iterable**
You can make an object iterable by giving it a `Symbol.iterator` method. The example below creates an object that returns values 10, 20, 30, 40 ... and stops at 100: [S1]
```javascript
myNumbers = {};
myNumbers[Symbol.iterator] = function() {
let n = 0;
done = false;
return {
next() {
n += 10;
if (n == 100) {done = true}
return {value:n, done:done};
}
};
}
for (const num of myNumbers) {
// Any Code Here
}
```
The `next()` method returns an object with two properties: `value` (the next value) and `done` (`true` when iteration is finished). [S1]
## 🛠️ 적용 사례 (Applied in summary)
The page's own snippets are the canonical applied examples — looping over the string `"W3Schools"`, the array `["a","b","c"]`, and the home-made `myNumbers` iterable built with `Symbol.iterator`. No external project/commit applications found in the source.
## 💻 코드 패턴 (Code patterns)
Loop over any iterable (language: JavaScript):
```javascript
for (const x of "W3Schools") {
// code block to be executed
}
```
Make a plain object iterable:
```javascript
myNumbers[Symbol.iterator] = function() {
let n = 0;
done = false;
return {
next() {
n += 10;
if (n == 100) {done = true}
return {value:n, done:done};
}
};
}
```
## ⚖️ 모순 및 업데이트 (Contradictions & updates)
No contradictions found in the source.
## ✅ 검증 상태 및 신뢰도
- **상태:** draft
- **검증 단계:** conceptual (실제 적용 사례 발견 시 applied/validated로 승격 가능)
- **출처 신뢰도:** B (W3Schools — widely used educational reference, not a primary standards body)
- **신뢰 점수:** 0.88
- **중복 검사 결과:** 신규 생성 (New discovery)
## 🔗 지식 그래프 (Knowledge Graph)
- **상위/루트:** [[JavaScript Tutorial]]
- **관련 개념:** [[JavaScript Iterators]], [[JavaScript Generators]], [[JavaScript Symbols]]
- **참조 맥락:** Referenced whenever using `for...of` loops or building custom data structures that should be loopable.
## 📚 출처 (Sources)
- [S1] W3Schools — JavaScript Iterables — https://www.w3schools.com/js/js_iterables.asp
## 📝 변경 이력 (Change history)
- 2026-06-23: Initial draft synthesized from the W3Schools "JavaScript Iterables" page (Astra wiki-curation, P-Reinforce v3.1 format).