Course contentsShow
← JavaScript
Lesson 1079 of 2,72325. Iterators and GeneratorsFree lesson

Iterators Are One-Time Use

Why calling Symbol.iterator again creates a fresh iterator instance.

Iterators Are One-Time Use

You'll learn why iterators can only be traversed once and how to get a fresh iterator when needed.

The Core Idea

Once an iterator has been exhausted (all its values consumed), it's done. Calling next() again will just keep returning {value: undefined, done: true}. To iterate through the same data again, you need to call Symbol.iterator on the original iterable to get a brand-new iterator instance.

Why Iterators Are Disposable

Think of an iterator like a DVD player's play-through: once you've watched to the end, the player stays at "finished." To watch again, you need to restart from the beginning—but with iterators, you can't rewind. Instead, you get a fresh iterator.

const arr = [1, 2, 3];
const iterator = arr[Symbol.iterator]();

console.log(iterator.next()); // {value: 1, done: false}
console.log(iterator.next()); // {value: 2, done: false}
console.log(iterator.next()); // {value: 3, done: false}
console.log(iterator.next()); // {value: undefined, done: true}
console.log(iterator.next()); // {value: undefined, done: true} — still done!

Getting a Fresh Start

To iterate again, call Symbol.iterator on the iterable (not the exhausted iterator):

const arr = [1, 2, 3];
const firstIterator = arr[Symbol.iterator]();
console.log([...firstIterator]); // [1, 2, 3]

// firstIterator is now exhausted
const secondIterator = arr[Symbol.iterator]();
console.log([...secondIterator]); // [1, 2, 3] — fresh iterator!

Each call to Symbol.iterator returns a new iterator with its own independent state.

Key Takeaway: Iterators are one-time use—once exhausted, they stay exhausted. To iterate again, call Symbol.iterator on the original iterable to create a fresh iterator instance.