JS Iterator Pattern
Traverse a collection sequentially without exposing its internal structure
When to Use
- You need to iterate over a custom data structure (tree, graph, linked list) with
for...of
- You want to provide lazy, on-demand values without generating the entire collection upfront
- Building data pipelines that compose iterables (filter, map, take)
Instructions
- Implement the iterator protocol: an object with a
next() method returning { value, done }.
- Make a collection iterable by adding
[Symbol.iterator]() that returns an iterator.
- Use generator functions (
function*) for the cleanest iterator implementation.
- Use
for...of to consume iterables.
class Range {
constructor(start, end) {
this.start = start;
this.end = end;
}
[Symbol.iterator]() {
let current = this.start;
const end = this.end;
return {
next() {
return current <= end
? { value: current++, done: false }
: { value: undefined, done: true };
},
};
}
}
for (const n of new Range(1, 3)) {
console.log(n); // 1, 2, 3
}
- Generator shorthand:
*[Symbol.iterator]() { for (let i = this.start; i <= this.end; i++) yield i; }.
Details
JavaScript has a built-in iteration protocol. Any object with a [Symbol.iterator]() method is iterable and works with for...of, spread (...), destructuring, Array.from(), and Promise.all(). Generators (function*) are the simplest way to create custom iterators.
Trade-offs:
- Custom iterators add complexity — only worthwhile for non-trivial data structures
- Infinite iterators (no
done: true) will hang for...of loops unless guarded
- Generator objects are single-use — once exhausted, they cannot be reset
When NOT to use:
- For arrays and built-in collections — they are already iterable
- When you need random access — iterators are sequential by design
Source
https://patterns.dev/javascript/iterator-pattern
Process
- Read the instructions and examples in this document.
- Apply the patterns to your implementation, adapting to your specific context.
- Verify your implementation against the details and edge cases listed above.
Harness Integration
- Type: knowledge — this skill is a reference document, not a procedural workflow.
- No tools or state — consumed as context by other skills and agents.
Success Criteria
- The patterns described in this document are applied correctly in the implementation.
- Edge cases and anti-patterns listed in this document are avoided.
1---2name: js-iterator-pattern3description: JS Iterator Pattern4metadata:5 source: https://skillmd.com/skills/intense-visions/js-iterator-pattern6---7# JS Iterator Pattern89> Traverse a collection sequentially without exposing its internal structure1011## When to Use1213- You need to iterate over a custom data structure (tree, graph, linked list) with `for...of`14- You want to provide lazy, on-demand values without generating the entire collection upfront15- Building data pipelines that compose iterables (filter, map, take)1617## Instructions18191. Implement the iterator protocol: an object with a `next()` method returning `{ value, done }`.202. Make a collection iterable by adding `[Symbol.iterator]()` that returns an iterator.213. Use generator functions (`function*`) for the cleanest iterator implementation.224. Use `for...of` to consume iterables.2324```javascript25class Range {26 constructor(start, end) {27 this.start = start;28 this.end = end;29 }3031 [Symbol.iterator]() {32 let current = this.start;33 const end = this.end;34 return {35 next() {36 return current <= end37 ? { value: current++, done: false }38 : { value: undefined, done: true };39 },40 };41 }42}4344for (const n of new Range(1, 3)) {45 console.log(n); // 1, 2, 346}47```48495. Generator shorthand: `*[Symbol.iterator]() { for (let i = this.start; i <= this.end; i++) yield i; }`.5051## Details5253JavaScript has a built-in iteration protocol. Any object with a `[Symbol.iterator]()` method is iterable and works with `for...of`, spread (`...`), destructuring, `Array.from()`, and `Promise.all()`. Generators (`function*`) are the simplest way to create custom iterators.5455**Trade-offs:**5657- Custom iterators add complexity — only worthwhile for non-trivial data structures58- Infinite iterators (no `done: true`) will hang `for...of` loops unless guarded59- Generator objects are single-use — once exhausted, they cannot be reset6061**When NOT to use:**6263- For arrays and built-in collections — they are already iterable64- When you need random access — iterators are sequential by design6566## Source6768https://patterns.dev/javascript/iterator-pattern6970## Process71721. Read the instructions and examples in this document.732. Apply the patterns to your implementation, adapting to your specific context.743. Verify your implementation against the details and edge cases listed above.7576## Harness Integration7778- **Type:** knowledge — this skill is a reference document, not a procedural workflow.79- **No tools or state** — consumed as context by other skills and agents.8081## Success Criteria8283- The patterns described in this document are applied correctly in the implementation.84- Edge cases and anti-patterns listed in this document are avoided.