Lesson 39: Mastering JavaScript Iterable with challenges!

What is an Iterable?
An iterable is any object in JavaScript that can be used in a for..of
loop. It follows a specific protocol to allow iteration over its values.
Protocol:
To be iterable, an object must implement the Symbol.iterator
method, which returns an iterator.
Iterator:
An iterator is an object that provides a next()
method which returns an object of the form:
jsCopyEdit{ value: ..., done: true/false }
Built-in Iterables:
Strings
Arrays
Maps
Sets
Typed Arrays
Many DOM collections
Key Use Case:
jsCopyEditfor (let item of iterable) {
// Automatically uses iterable[Symbol.iterator]()
}
βοΈ 2. Internal Mechanics
Step-by-step: How for..of
Works Internally
Calls the object's
[Symbol.iterator]()
method.That returns an iterator object.
Then
for..of
repeatedly callsiterator.next
()
until{ done: true }
.
Example:
jsCopyEditlet range = {
from: 1,
to: 5,
[Symbol.iterator]() {
this.current = this.from;
return this;
},
next() {
if (this.current <= this.to) {
return { done: false, value: this.current++ };
} else {
return { done: true };
}
}
};
for (let num of range) {
console.log(num); // 1, 2, 3, 4, 5
}
π΅οΈββοΈ 3. Hidden Quirks & Pitfalls
1. No simultaneous iteration if object is its own iterator:
jsCopyEditlet iter = range[Symbol.iterator]();
let iter2 = range[Symbol.iterator]();
console.log(iter.next()); // Works
console.log(iter2.next()); // Same shared state β may be broken or overlap
2. Infinite Iterators: Dangerous in for..of
!
jsCopyEditlet infinite = {
[Symbol.iterator]() {
return {
next() {
return { done: false, value: Math.random() };
}
};
}
};
// for (let val of infinite) break; // would run forever without break
π’ Basic
Iterate Over a String
Create a loop that prints each character in the string"Mastery"
usingfor...of
. Donβt use.split()
.Manual Iterator from Array
Manually extract and log the first 3 elements from an array using its[Symbol.iterator]
directly.Array-like to Iterable
Convert an array-like object (with numeric keys and length) into an iterable object so it can be used in afor...of
loop.
π‘ Intermediate
Custom Range Iterable
Build an object that behaves likerange(3, 7)
and supportsfor...of
, yielding numbers from 3 to 7.Pauseable Iterator
Create a custom iterable that yields 5 numbers, but allows external control to pause and resume iteration.Filter During Iteration
Create an iterable that filters out odd numbers during iteration (only even numbers should be yielded from a given array).Iterable Reverse Proxy
Make a string iterable that yields characters in reverse order via a custom iterator.
π΄ Advanced
Composable Iterables
Create a functioncombine(iter1, iter2)
that merges two iterable sequences into one (like zip or concat).Memoized Iterator
Build an iterator that remembers all previously yielded values and allows "replay" of iteration from the start.Async Iterable Bridge
Create a sync iterable wrapper over an async generator (simulate paginated API results in syncfor...of
).
π― Bonus Brain-Twister
- Two-way Iterator Trap
Create a custom iterable where:
Forward loop yields characters in order.
Reverse loop (when used with a flag) yields characters in reverse.
You must use only one
[Symbol.iterator]()
but behave differently depending on the flag.
πΉ 4. Interview-Ready Questions
Mix of conceptual, scenario-based, and debugging-style questions. Covers real-world cases and common pitfalls.
π Conceptual Questions
What is the difference between an iterable and an iterator in JavaScript?
Why can't we use
for...of
on all objects? How can we make a plain object iterable?How does JavaScript's
for...of
loop internally know when to stop?
π Scenario-Based Questions
You have an object with numeric keys and a length property, but
for...of
throws a TypeError. What could be the reason?You need to iterate over DOM
NodeList
elements β how do you convert them to a real array and explain why that might be helpful?You're reading a paginated API and want to load each page lazily. How can you design a custom iterable to handle that?
π Debugging-Style Questions
You created a custom iterable, but it only yields one value and stops. What might be wrong in your
next()
implementation?Your
for...of
loop seems to be stuck in an infinite loop β what likely went wrong in your custom iterator?You define
[Symbol.iterator]
on an object butfor...of
still doesn't work. What could be missing?Youβre using
Array.from(customIterable)
but get an empty array β what should you inspect?
π 5. Real-World Usage
β Looping over:
DOM NodeLists
Custom data ranges
Paginated APIs (e.g., via async iterators)
β
Use Array.from()
to:
Convert NodeList β Array
Convert Set β Array
Apply mapping during conversion
jsCopyEditArray.from("abc", c => c.toUpperCase()); // ["A", "B", "C"]
6.Mind Map (Text Version)
typescriptCopyEditIterable
β
βββ [Symbol.iterator] method
β βββ Returns an Iterator object
β βββ next() β {value, done}
β
βββ Built-in Iterables
β βββ String
β βββ Array
β βββ Map, Set, TypedArray, etc.
β
βββ Custom Iterables
β βββ Implement [Symbol.iterator]
β βββ Return iterator with next()
β
βββ Array-like vs Iterable
β βββ Array-like β { 0: ..., length: ... }
β βββ Iterable β has Symbol.iterator
β
βββ Utilities
βββ Array.from()
βββ Converts iterable/array-like to Array
βββ Optional mapping function
π 7. Fun Application
β¨ Build a blinking text animation using a custom iterable:
jsCopyEditlet textBlinker = {
text: "π‘JavaScriptπ‘",
[Symbol.iterator]() {
let visible = true;
return {
next: () => ({
done: false,
value: visible ? this.text : " ".repeat(this.text.length),
toggle: () => visible = !visible
})
};
}
};
let iter = textBlinker[Symbol.iterator]();
setInterval(() => {
let result = iter.next();
console.clear();
console.log(result.value);
result.toggle();
}, 500);
Subscribe to my newsletter
Read articles from manoj ymk directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
