for...of Loop — JS Technical Interview Questions & Answers

Iterating over iterable values — arrays, strings, Maps, Sets.

for...of Loop

  • Iterating over iterable values — arrays, strings, Maps, Sets.
  • for...of Loop is JavaScript control flow. if / switch pick a branch. for / while repeat. Condition must be boolean-ish: if (marks >= 40). Use === not =.
  • for (let i = 0; i < arr.length; i++) or for (const m of marks). while needs a change inside or it loops forever.
  • Trap: = inside if. Trap: off-by-one on the last index.

Technical Interview Tip: for...of Loop: tiny marks example, predict the path.

Code Examples for for...of Loop

for...of Loop — working example

for (let i = 1; i <= 3; i++) {
  if (i === 2) continue;
  console.log("for", i);
}

let n = 3;
while (n > 0) {
  console.log("while", n);
  n -= 1;
}

const user = { name: "Ada", city: "Pune" };
for (const key in user) console.log(key, user[key]);

for (const ch of "JS") console.log(ch);

for...of Loop — for/while/do-while repeat work; for...in walks keys; for...of walks iterable values.

How to Explain for...of Loop to an Interviewer

When for...of Loop?

To branch or repeat Show one small snippet