Example 1 — Basic for...of Loop
Call entries() and log each pair as the iterator advances.
const colors = ["red", "green", "blue"];
const iterator = colors.entries();
for (const entry of iterator) {
console.log(entry);
}
// [0, "red"]
// [1, "green"]
// [2, "blue"] How It Works
- Build the array. Three color strings at indexes 0, 1, and 2.
- Create the iterator.
entries()returns a lazy walker — not a new array yet. - Pull each pair.
for...ofasks for the next[index, value]until done. - No mutation.
colorsis unchanged after the loop.

