Manually Calling next() on an Iterator
You'll learn how to take direct control of iteration by manually advancing through an iterator's values.
Beyond for...of Automation
When you use for...of, JavaScript automatically calls the iterator's next() method behind the scenes. But you can call next() yourself to get fine-grained control over when and how you advance through a collection.
How It Works
Every iterator has a next() method that returns an iterator result object with two properties:
value: the current itemdone: a boolean indicating whether iteration is finished
By calling next() manually, you decide exactly when to move forward:
const colors = ['red', 'green', 'blue'];
const iterator = colors[Symbol.iterator]();
console.log(iterator.next()); // { value: 'red', done: false }
console.log(iterator.next()); // { value: 'green', done: false }
console.log(iterator.next()); // { value: 'blue', done: false }
console.log(iterator.next()); // { value: undefined, done: true }
Why Call next() Manually?
Think of it like a book: for...of reads every page automatically from start to finish. Manual next() calls let you read one page, pause to think, maybe skip ahead, or stop reading entirely based on what you just read.
const numbers = [10, 20, 30, 40];
const iter = numbers[Symbol.iterator]();
let result = iter.next();
while (!result.done && result.value < 30) {
console.log(result.value); // 10, 20
result = iter.next();
}
// Stopped early based on a condition
Key Takeaway: Manually calling next() gives you precise control over iteration, letting you pause, inspect values, and decide whether to continue advancing through the sequence.