JavaScript Notes
Master JavaScript iterators: iterator protocol, Symbol.iterator, custom iterables, for...of loop, generators as iterators, lazy evaluation, and interview Q&A.
What are Iterators?
An iterator is any object that knows how to access items in a collection one at a time. It follows the Iterator Protocol: it must have a .next() method that returns { value, done } objects.
An iterable is any object that knows how to create an iterator — it must implement the [Symbol.iterator]() method that returns an iterator.
One-liner: An iterator is an object with .next() that gives you one item at a time. An iterable is an object that can create an iterator.The Two Protocols
Iterator Protocol
An object is an iterator if it has:
{
next() {
return { value: any, done: boolean };
}
}value: the current item (orundefinedwhen done)done:falsewhile items remain,truewhen exhausted
Iterable Protocol
An object is an iterable if it has:
Built-in Iterables
JavaScript's built-in iterables:
{ value: 1, done: false }
{ value: 2, done: false }
{ value: 3, done: false }
{ value: undefined, done: true }How for...of Uses the Iterator Protocol
apple mango banana
Internally, for...of does this:
Iterator Protocol Visualization
Creating a Custom Iterator
{ value: 1, done: false }
{ value: 2, done: false }
{ value: 3, done: false }
{ value: 4, done: false }
{ value: undefined, done: true }Creating a Custom Iterable Object
To make an object work with for...of, add [Symbol.iterator]():
1 2 3 4 5 [1, 2, 3, 4, 5]
Self-Referential Iterable (Iterator + Iterable)
An object can be both the iterable AND the iterator by returning this from [Symbol.iterator]():
10 11 12 13
Generators as Iterators
Generators automatically implement the iterator protocol:
{ value: 1, done: false }
1
2
3
4
5Iterating Over Different Built-ins
// String iterator
for (const char of "WoHo") {
console.log(char);
}W o H o
a: 1 b: 2
10 20 30
Infinite Lazy Iterator
[0, 1, 2, 3, 4]
Iterators vs Generators — Comparison
| Feature | Custom Iterator | Generator |
|---|---|---|
| Syntax | Manual object with .next() | function* with yield |
| Code complexity | More verbose | Very concise |
| State management | Manual variables | Engine handles it |
| Lazy evaluation | ✅ | ✅ |
| Best for | Simple/precise control | Complex sequences |
Common Mistakes
❌ Mistake 1 — Forgetting [Symbol.iterator] on Custom Object
TypeError: obj is not iterable
❌ Mistake 2 — Reusing Exhausted Iterator
1 2 3
(second loop produces nothing — iterator is exhausted)
❌ Mistake 3 — Spreading Infinite Iterator
Always use break or take-style limiting when consuming infinite iterators.
Interview Questions 🎯
Q1. What is the Iterator Protocol in JavaScript?
The Iterator Protocol defines that an object is an iterator if it has a .next() method that returns { value: any, done: boolean }. When done is true, the iterator is exhausted.
Q2. What is the difference between an iterable and an iterator?
- Iterable: An object with a
[Symbol.iterator]()method that returns an iterator. Examples: Array, String, Map, Set. - Iterator: An object with a
.next()method that returns{ value, done }. It maintains position state.
An iterable can produce multiple independent iterators. An iterator tracks one traversal position.
Q3. How does for...of use the iterator protocol?
for...of first calls [Symbol.iterator]() to get an iterator, then repeatedly calls .next() and uses the value from each result. It stops when done: true is returned.
Q4. How do you make a custom object iterable?
Add a [Symbol.iterator]() method that returns an object with a .next() method:
10 20 30
Q5. What is the difference between an iterator and a generator?
A generator is a special function (function*) that automatically creates an iterator. Every generator is an iterator (it has .next() and [Symbol.iterator]()), but not every iterator is a generator (you can manually implement one).
Q6. What built-in JavaScript types are iterable?
Array, String, Map, Set, TypedArray, arguments object, NodeList, and any object implementing [Symbol.iterator]. Plain objects {} are NOT iterable by default.
Q7. What is lazy evaluation in the context of iterators?
Lazy evaluation means values are computed on demand — only when .next() is called. This allows infinite sequences to exist in memory efficiently, since values are produced one at a time rather than all at once.
Q8. How can you create an infinite iterator safely?
Use an infinite iterator with a break statement in for...of, or implement a take(n) utility:
function take(iterable, n) {
const result = [];
for (const item of iterable) {
result.push(item);
if (result.length >= n) break;
}
return result;
}Key Takeaways 🔑
- Iterator: Object with
.next()returning{ value, done } - Iterable: Object with
[Symbol.iterator]()returning an iterator for...of, spread (...), and destructuring all use the iterator protocol- Built-in iterables: Array, String, Map, Set, TypedArray
- Plain objects are NOT iterable by default — add
[Symbol.iterator] - Generators automatically implement the iterator protocol
- Iterators enable lazy evaluation — infinite sequences are safe because values are produced on demand
- Once exhausted (
done: true), create a new iterator — don't reuse
Summary
The Iterator Protocol is the foundation of how JavaScript handles sequential data access. An iterable exposes a [Symbol.iterator]() factory method; an iterator tracks position and has a .next() method returning { value, done }. All built-in collection types (Array, String, Map, Set) implement this protocol, enabling for...of, spread, and destructuring. Custom iterables can be created by implementing [Symbol.iterator], and generators offer a concise shorthand for creating iterators. The protocol's power lies in lazy evaluation — values are produced only when requested, enabling infinite sequences and memory-efficient data pipelines.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for JavaScript Iterators - Complete Guide with Iterator Protocol.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this JavaScript Master Course topic.
Search Terms
javascript-complete-guide, javascript master course, javascript, complete, guide, advanced, iterators, javascript iterators - complete guide with iterator protocol
Related JavaScript Master Course Topics