Loops in JavaScript: for, while & for...of

Easy100% Free~12 mins#loops#for-loop#while#var-vs-let#labels
Interview Answer (30-Second Summary)

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}
7
8let n = 0;
9while (n < 3) n++;
10console.log('while', n);
11
12let m = 5;
13do {
14 m++;
15} while (m < 3);
16console.log('do', m);
17
18outer: 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
Interactive Challenge

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.

Call Stack (Top = Active)
Global
Lexical Scope / Bindings
i (one shared binding):0 -> 3
Microtask Queue (0)
Macrotask Queue (3)
timer: var
timer: var
timer: var
Console Stream
> [empty]

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).

  1. var vs let in a for loop: var is function-scoped, so the whole loop shares one i; callbacks that run later all see its final value, 3. let is block-scoped and the for loop creates a new binding per iteration, so each callback sees 0, 1 and 2.
  2. Condition timing: while may run zero times; do...while always runs at least once.
  3. Controlling nested loops: break leaves the current loop and continue skips to its next pass. A label (outer:) lets continue outer or break outer act on the outer loop. Array methods such as forEach cannot be stopped with break; use for...of, some or find when 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.

Q1.
What are the loops in JavaScript?

for, while, do...while, for...of and for...in. Arrays also have iteration methods such as forEach, map and some, which take a callback instead of a loop body.

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.

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.

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.

Put a label before the outer loop (outer: for ...) and use break outer or continue outer inside the inner loop.

No. forEach always visits every element. Use for...of with break, or some/every/find, which stop when the callback returns the right value.

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