Skip to chapter content
navan✳THE BUILD LABJAVASCRIPT
0/31 read
CHAPTER 08 / 31FLOW AND SCOPE

Loops are repeated decisions.

Choose a loop by what changes and what ends the repetition.

01 / THE BASICS

Start here.

A loop repeats instructions. Imagine printing the letters in 'cat'. Writing three separate console.log calls works for three letters, but a loop can describe the repeated rule once. In for (const letter of 'cat'), JavaScript takes one letter at a time from the string. On each pass, letter names the current value and the block runs. After the last letter, the loop ends. This form is called for...of.

A while loop has a different shape: while (condition) { ... }. It checks the condition before each pass. If the condition is false at the beginning, the body never runs. The body usually changes something that eventually makes the condition false. A traditional for loop puts initialization, condition, and update together: for (let i = 0; i < 3; i++). Here i starts at zero, the body runs while i is less than three, and i increases after each pass.

The boundary matters. Starting at zero and stopping before three produces indices 0, 1, and 2. An array with three elements uses those positions. If the update never moves toward a false condition, a loop can continue forever. When reading a loop, identify its starting value, what happens once per pass, and the exact reason it stops. That reasoning works even when the body is much longer.

02 / THE IDEA

Build the model.

A loop repeats a block while a condition or sequence allows it. A while loop foregrounds the stopping condition; for...of foregrounds the values in an iterable; a traditional for loop foregrounds an index. These are different ways to express the same control question.

Before reading a loop's body, find its invariant: what remains true at the start of each pass? Then find what advances toward termination. Infinite loops often come from a condition that never changes; off-by-one errors come from including or excluding the boundary incorrectly.

03 / UNDER THE SURFACE

How it works.

Every loop needs three ideas: a starting state, a rule for another pass, and progress toward stopping. In a traditional for loop these often sit together as initialization, condition, and update. In while, you supply the state change inside the body. In for...of, the iterable supplies the sequence and ending, so you focus on each value rather than an index.

Choose the form by what the reader needs. Use for...of when each element matters and positions do not. Use an indexed for loop when a position or neighboring element is part of the algorithm. Use while when the number of passes is not known in advance. Before running a loop, trace a tiny input by hand and check the first pass, the final pass, and the first failing condition.

04 / A WORKED EXAMPLE

Read it line by line.

JAVASCRIPT / Worked example
for (const letter of 'cat') {
  console.log(letter);
}

First notice. The iterable supplies values; no manual index is needed.

The string 'cat' is iterable. for...of asks it for the first value, 'c', assigns that to letter, and runs the body. It repeats for 'a' and 't'. When the iterator reports no more values, the loop ends. The binding letter is fresh for each pass and does not need a manual counter.

05 / ANOTHER EXAMPLE

See the rule again.

JAVASCRIPT / Another example
let total = 0;
for (const number of [2, 3, 4]) {
  total += number;
}
console.log(total);

total starts at 0, becomes 2, then 5, then 9. The array has no more elements, so the loop stops and prints 9. Notice the invariant: before each pass, total equals the sum of values already visited.

06 / KEEP THIS

One thing to remember.

A readable loop makes its stopping rule obvious.

Finished this chapter?
Built with curiosity. By Navan Sami. ↗JavaScript · Understand the language behind the interface.