--- 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).