Start here.
Iteration means visiting values one after another. An array is a familiar sequence, but strings, sets, maps, and other values can also supply a sequence. for...of asks a value for its iterator and then repeatedly asks that iterator for the next result. Each result has a value and a done flag. When done becomes true, the loop stops. The loop hides those messages so you can focus on each value.
This agreement is called a protocol: a shape of behavior that different kinds of value can provide. An iterable supplies a function at a special key named Symbol.iterator. Calling that function gives an iterator with a next method. You usually do not write these pieces by hand, but the model explains why for...of works on a string and why spreading a string into an array produces its characters.
A plain object is not automatically iterable just because it has properties. Object.keys(object) first makes an array of property names, which can then be visited with for...of. Iteration is about an ordered sequence of values, not simply having data inside. When reading a loop, ask what sequence is being supplied and whether the loop needs the values, the positions, or both.
Build the model.
for...of does not require an array. It asks a value for an iterator, then repeatedly asks that iterator for a next result until it reports done. Strings, arrays, maps, sets, and generators participate in this protocol.
The protocol explains why spread works for an array but not for every plain object in an array position. It also separates a collection from the act of walking through it. Some iterators produce values only once; a generator can create them on demand.
How it works.
An iterable is a value that knows how to provide an iterator. The iterator has a next method that produces an object containing a value and a done flag. for...of repeatedly calls next, assigns each value to the loop binding, and stops when done is true. You rarely call this protocol directly, but recognizing it explains why the same loop works for arrays, strings, sets, and generators.
Array spread also consumes an iterable: [...source] asks for every value and places it into a new array. This can exhaust a one-time iterator. A plain object is not automatically iterable for array spread, even though object spread can copy its properties; those two forms use different rules. Use iteration when you need a sequence, and property access when you need named fields.
Read it line by line.
const letters = [...'fox'];
for (const value of new Set([1, 1, 2])) {
console.log(value);
}First notice. Spread consumes the string iterator; Set iteration yields 1 and 2.
The string iterator yields 'f', then 'o', then 'x'; spread collects those values into letters. The Set constructor receives [1, 1, 2] but keeps unique primitive values, so its iterator yields 1 and 2. The for...of body runs twice. Nothing in for...of requires numeric indexes.
See the rule again.
const unique = new Set(['a', 'a', 'b']);
const letters = [...unique];
console.log(letters);The output is ['a', 'b']. Set stores each unique string once. Spread consumes the Set iterator and writes each yielded value into a new array. The Set itself remains available and unchanged.
One thing to remember.
Iterable means a value can provide a sequence, not that it is an array.