Example 1 — Loop Indexes with for...of
Use keys() when you care about the index, not just the value.
const langs = ["JS", "Python", "Java"];
for (const i of langs.keys()) {
console.log(i + ": " + langs[i]);
} How It Works
- Iterator.
langs.keys()yields0, then1, then2. - for...of. Each loop step receives the next index.
- Read value. Use
langs[i]when you need the element too. - No mutation. The array stays unchanged.

