Loops in JavaScript: for, while & for...of
Loops in JavaScript repeat code: for runs a counter loop, while repeats while a condition holds, do...while runs its body at least once, for...of walks the values of an iterable and for...in walks an object's keys. In a for loop, let gives each iteration its own variable, while var shares one, which matters when callbacks run later.
Key Learning Objectives
Write for, while, do...while, for...of and for...in loops and know when each fits.
Explain why var and let behave differently in a loop with callbacks.
Use break, continue and labels to control nested loops.
Know that a do...while body always runs at least once.
The Interview Problem
In what order are the ten lines printed?
1for (var i = 0; i < 3; i++) {2 setTimeout(() => console.log('var', i), 0);3}4for (let j = 0; j < 3; j++) {5 setTimeout(() => console.log('let', j), 0);6}78let n = 0;9while (n < 3) n++;10console.log('while', n);1112let m = 5;13do {14 m++;15} while (m < 3);16console.log('do', m);1718outer: for (const a of [1, 2]) {19 for (const b of [1, 2]) {20 if (b === 2) continue outer;21 console.log('pair', a, b);22 }23}
Predict Console Output
Select the option that matches what this code prints to the console:
while 3 / do 6 / pair 1 1 / pair 2 1 / var 3 / var 3 / var 3 / let 0 / let 1 / let 2
var 0 / var 1 / var 2 / let 0 / let 1 / let 2 / while 3 / do 6 / pair 1 1 / pair 2 1
while 3 / do 5 / pair 1 1 / pair 2 1 / var 3 / var 3 / var 3 / let 0 / let 1 / let 2
while 3 / do 6 / pair 1 1 / pair 1 2 / pair 2 1 / pair 2 2 / var 3 / var 3 / var 3 / let 0 / let 1 / let 2
V8 Engine Execution Trace
Step 1 of 7 (Line 2)The var loop schedules three timers. There is only one i for the whole loop, and it is 3 when the loop ends.
Loops in JavaScript Explained
The Loops in JavaScript and When to Use Each
Loops in JavaScript come in five kinds: for (counter loops), while (repeat while a condition holds), do...while (body first, then the check), for...of (values of an iterable such as an array or string) and for...in (keys of an object).
- var vs let in a for loop:
varis function-scoped, so the whole loop shares onei; callbacks that run later all see its final value, 3.letis block-scoped and theforloop creates a new binding per iteration, so each callback sees 0, 1 and 2. - Condition timing:
whilemay run zero times;do...whilealways runs at least once. - Controlling nested loops:
breakleaves the current loop andcontinueskips to its next pass. A label (outer:) letscontinue outerorbreak outeract on the outer loop. Array methods such asforEachcannot be stopped withbreak; usefor...of,someorfindwhen you need to stop early.
Common Traps & Mistakes
Using var in a loop whose callbacks run later, and expecting each callback to see its own index.
Using for...in on an array: it gives string keys and can include inherited properties.
Trying to break out of forEach, which has no way to stop early.
FAANG Follow-Up Probes
Probe #1
Fix the var loop so it prints 0, 1, 2 without changing var to let.
Probe #2
When would you choose while over for?
Probe #3
How would you loop over an object's keys and values?
Loops in JavaScript Interview Questions
Key interview inquiries, edge cases, and architectural follow-ups for Loops.
What is the difference between for...of and for...in?
for...of gives the values of an iterable (arrays, strings, Maps, Sets). for...in gives the enumerable string keys of an object, including inherited ones.
Why does a var loop with setTimeout print the same number three times?
var creates one variable for the whole loop. The callbacks run after the loop has finished, so they all read its final value. let creates a new binding per iteration, so each callback keeps its own value.
What is the difference between while and do...while?
while checks the condition before each pass and may never run. do...while runs the body first and checks afterwards, so it always runs at least once.
How do you break out of a nested loop?
Put a label before the outer loop (outer: for ...) and use break outer or continue outer inside the inner loop.
Can you use break inside forEach?
No. forEach always visits every element. Use for...of with break, or some/every/find, which stop when the callback returns the right value.
Which loop is fastest in JavaScript?
For everyday code the difference is negligible; modern engines optimise all of them. Choose the loop that reads most clearly, and measure if a hot path really matters.
More JavaScript interview questions for freshers
