--- id: javascript-iterators title: "JavaScript Iterators" category: "Frontend" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["JS iterators", "iterator protocol", "next()", "Iterator helpers", "Iterator.from"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.87 created_at: 2026-06-23 updated_at: 2026-06-23 review_reason: "" merge_history: [] tags: ["javascript", "js", "web", "frontend", "w3schools", "iterators", "iterator-helpers", "es2025"] raw_sources: ["https://www.w3schools.com/js/js_iterators.asp"] applied_in: [] github_commit: "" --- # [[JavaScript Iterators]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) An iterator is an object that provides a standard way to access elements sequentially through a `next()` method; ES2025 adds iterator helper methods (`map`, `filter`, `reduce`, `take`, `drop`, etc.) and `Iterator.from()` that bring iteration directly into the core language. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **Iterator = sequential access object** β€” an iterator is an object that provides a standard way to access elements one after another. [S1] - **The iterator protocol** β€” an iterator must implement a `next()` method that returns an object with `value` (the next value) and `done` (`false` while more elements exist, otherwise `true`). [S1] - **`for...of` consumes iterables** β€” the `for...of` statement loops through the elements of an iterable object; iterables must implement `Symbol.iterator`. Built-in iterables are Strings, Arrays, Typed Arrays, Sets, and Maps. [S1] - **Iterator helper methods (ES2025)** β€” new helpers let you transform and consume iterators lazily: `drop`, `every`, `filter`, `find`, `flatMap`, `forEach`, `from`, `map`, `reduce`, `some`, `take`. [S1] ## 🧩 μΆ”μΆœλœ νŒ¨ν„΄ (Extracted patterns) - **`Iterator.from(iterable)`** β€” wrap any iterable into an iterator object so the helper methods become available. [S1] - **Lazy chaining** β€” methods like `filter`, `map`, `take`, and `drop` return a new iterator (not an array), enabling pipeline-style transformation of sequences. [S1] - **Terminal reducers** β€” `every`, `some`, `find`, `reduce`, and `forEach` consume the iterator to produce a single value or side effect. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) **JavaScript Iterators** An iterator is an object that provides a standard way to access elements sequentially. Iterators must adhere to the iterator protocol by implementing a `next()` method. [S1] **The `next()` Method** The `next()` method returns an object with two properties: `value` holds the next value in the iteration sequence, and `done` returns `false` if there are more elements, otherwise `true`. [S1] **The `for...of` Loop** The JavaScript `for...of` statement loops through the elements of an iterable object. Iterables must implement the `Symbol.iterator` method. In JavaScript the following are iterables: Strings, Arrays, Typed Arrays, Sets, and Maps β€” their prototypes have a `Symbol.iterator` method. [S1] **`Iterator.from()`** β€” creates an iterator object from an iterable: [S1] ```javascript const myIterator = Iterator.from([1, 2, 3]); let text = ""; for (const x of myIterator) { text += x; } ``` **`drop()`** β€” returns an iterator that skips a specified number of elements before yielding the rest: [S1] ```javascript const myIterator = Iterator.from([1, 2, 3, 4, 5, 6]); const firstFive = myIterator.drop(5); ``` **`every()`** β€” returns `true` if all elements satisfy a test function: [S1] ```javascript const myIterator = Iterator.from("123456789"); let result = myIterator.every(x => x > 7); ``` **`filter()`** β€” returns an iterator containing elements that satisfy a filter function: [S1] ```javascript const myIterator = Iterator.from([32, 33, 16, 40]); const filteredIterator = myIterator.filter(x => x > 18); ``` **`find()`** β€” returns the first element that satisfies a test function: [S1] ```javascript const myIterator = Iterator.from([3, 10, 18, 30, 20]); let result = myIterator.find(x => x > 18); ``` **`flatMap()`** β€” returns an iterator by mapping each element and then flattening the results: [S1] ```javascript const myIterator = Iterator.from([1, 2, 3, 4, 5, 6]); const mappedIterator = myIterator.flatMap(x => [x, x * 10]); ``` **`forEach()`** β€” executes a function once for each element in the iterator: [S1] ```javascript const myIterator = Iterator.from("123456789"); let text = ""; myIterator.forEach (x => text += x); ``` **`map()`** β€” returns an iterator with all elements transformed by a map function: [S1] ```javascript const myIterator = Iterator.from("123456789"); const mappedIterator = myIterator.map(x => x * 2); ``` **`reduce()`** β€” applies a reducer function against each element to reduce it to a single value: [S1] ```javascript const myIterator = Iterator.from([175, 50, 25]); let result = myIterator.reduce(myFunc); ``` **`some()`** β€” returns `true` if at least one element satisfies a test function: [S1] ```javascript const myIterator = Iterator.from("123456789"); let result = myIterator.some(x => x > 7); ``` **`take()`** β€” returns an iterator that yields a specified number of elements: [S1] ```javascript const myIterator = Iterator.from([1, 2, 3, 4, 5, 6]); const firstFive = myIterator.take(5); ``` **Iterator Helper Methods (ES2025)** [S1] | Function | Description | |----------|-------------| | `drop()` | Returns an iterator that skips a specified number of elements before yielding the rest | | `every()` | Returns `true` if all elements satisfy a test function | | `filter()` | Returns an iterator containing elements that satisfy a filter function | | `find()` | Returns the first element that satisfies a test function | | `flatMap()` | Returns an iterator by mapping each element and then flattening the results | | `forEach()` | Executes a function once for each element in the iterator | | `from()` | Creates an iterator object from an iterable | | `map()` | Returns an iterator with all elements transformed by a map function | | `reduce()` | Applies a reducer function against each element to reduce it to a single value | | `some()` | Returns `true` if at least one element satisfies a test function | | `take()` | Returns an iterator that yields a specified number of elements | Iterators bring the iteration concept directly into the core JavaScript language and provide a mechanism for customizing the behavior of `for...of`. [S1] ## πŸ› οΈ 적용 사둀 (Applied in summary) The page's own `Iterator.from(...)` snippets are the canonical applied examples β€” wrapping arrays and strings into iterators and applying `filter`, `map`, `reduce`, `take`, `drop`, and the predicate helpers. No external project/commit applications found in the source. ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Wrap an iterable, then transform it (language: JavaScript): ```javascript const myIterator = Iterator.from([32, 33, 16, 40]); const filteredIterator = myIterator.filter(x => x > 18); ``` Reduce to a single value: ```javascript const myIterator = Iterator.from([175, 50, 25]); let result = myIterator.reduce(myFunc); ``` ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) The iterator helper methods (`map`, `filter`, `reduce`, `take`, `drop`, etc.) and `Iterator.from()` are an ES2025 addition; availability depends on the runtime supporting that version. [S1] ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual (μ‹€μ œ 적용 사둀 발견 μ‹œ applied/validated둜 승격 κ°€λŠ₯) - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.87 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[JavaScript Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[JavaScript Iterables]], [[JavaScript Generators]], [[JavaScript Symbols]] - **μ°Έμ‘° λ§₯락:** Referenced when consuming sequences with `for...of` or composing lazy data pipelines with the ES2025 iterator helpers. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” JavaScript Iterators β€” https://www.w3schools.com/js/js_iterators.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-06-23: Initial draft synthesized from the W3Schools "JavaScript Iterators" page (Astra wiki-curation, P-Reinforce v3.1 format).