Lesson content
Read, practise, then check your understanding
An iterator’s next() returns { value, done }. An iterable provides [Symbol.iterator]() returning an iterator. Arrays, strings, maps, sets, spread, destructuring, and for...of use this protocol.
Custom iterable
const countdown = {
from: 3,
*[Symbol.iterator]() {
for (let value = this.from; value > 0; value -= 1) yield value;
}
};
console.log([...countdown]);
Return a fresh iterator when multiple independent traversals are expected. Iterators may implement return for early-exit cleanup and throw for injected failure. Async iterables implement Symbol.asyncIterator and are consumed with for await...of.
Knowledge check
Answer every question correctly to complete this chapter.
0 of 10 checks passed
Your progress is saved on this device.