What for...of asks for
Run the examples in a modern browser developer console; they need no page elements or packages. When you write for (const value of [10, 20, 30]), JavaScript does not need to know that the source is an array. It asks the source for an iterator, then repeatedly asks that iterator for the next value. Strings, sets, maps, and arrays can all participate in this pattern.
An iterator has a next() method. Each call returns an object with a value and a done property. Try const iterator = [10, 20][Symbol.iterator](); followed by iterator.next() three times. The first two results have values 10 and 20 with done: false. The third has done: true; its value is not useful for this array. The iterator keeps its position, so calling next() does not restart the array.
Symbol.iterator is the method that makes a value iterable. It supplies an iterator when a loop begins. That distinction matters: an array can supply a new iterator for each loop, while an iterator you have already advanced may have values left to produce—or none at all.
Build an iterable that can restart
A custom iterable is useful when you want to define a sequence without first creating an array of its values. For example, this object produces three consecutive numbers: const range = { start: 4, end: 6, [Symbol.iterator]() { let current = this.start; const end = this.end; return { next() { if (current <= end) return { value: current++, done: false }; return { done: true }; } }; } };
Try for (const number of range) console.log(number); in the browser console. It prints 4, 5, and 6. Run the same loop again and it prints them again. Each call to range[Symbol.iterator]() creates a separate current variable, so the two loops do not share a position. If start is greater than end, the first call to next() reports done immediately.
This example also shows why a plain object is not automatically usable with for...of. It needs a Symbol.iterator method that returns an object with next(). Keep the method’s state local to each iterator when separate consumers should be able to traverse the sequence independently.
Use a generator to write the same sequence
Generator functions offer a shorter way to define an iterable sequence. They use function* and yield: function* numbersFrom(start, end) { for (let value = start; value <= end; value++) yield value; }. Try const sequence = numbersFrom(4, 6); and then call sequence.next() four times. You receive 4, 5, and 6 in successive results, followed by a result with done: true.
Calling a generator function creates a generator object; its body does not run through to completion at that moment. Each next() call resumes execution until the next yield or until the function finishes. You can also write for (const number of numbersFrom(4, 6)) console.log(number);. Calling numbersFrom(4, 6) again creates a fresh generator object, but looping over the already exhausted sequence does not restart it.
A generator’s return value behaves differently from a yielded value. If the function ends with return 'finished', a manual next() call can observe that value alongside done: true. A for...of loop stops when done is true, so it does not visit 'finished'. Use yield for values intended to be part of the sequence.
Choose between an array and a lazy sequence
An array is usually the simplest choice when you need a small collection that several parts of a program will inspect, index, or revisit. An iterator can be helpful when values are naturally produced in order and you may stop early. For example, a loop over numbersFrom(1, 1000000) can break after finding the first suitable number without building an array of a million entries. This is about avoiding unnecessary work, not a promise that every generator will be faster.
Some operations consume an iterable. For example, [...numbersFrom(1, 3)] creates an array containing 1, 2, and 3, while [...sequence] produces an empty array if sequence was already exhausted. Spreading a large or unbounded generator tries to collect all its values, which defeats the benefit of producing only what you need.
When debugging, check whether you are holding an iterable that creates fresh iterators or an iterator that tracks a single position. Start with a few manual next() calls, inspect both value and done, then try a for...of loop. That small test often makes a surprising empty loop easier to explain.



