beginner22 min

Loops

Repeat work with for and while loops, and control them with break and continue.

What you'll learn

  • Write a for loop to repeat an action a known number of times
  • Write a while loop to repeat while a condition holds
  • Use break and continue to control loop flow

Prerequisites

Explanation

Loops repeat a block of code so you don't have to write it out by hand. The for loop is the workhorse when you know roughly how many times you want to repeat something:

for (let i = 0; i < 5; i++) {
  console.log(i); // 0, 1, 2, 3, 4
}

A for loop has three parts separated by semicolons: an initializer (let i = 0), a condition checked before each pass (i < 5), and an update that runs after each pass (i++). The loop keeps running as long as the condition is true.

The while loop is simpler and better when you don't know the number of repetitions in advance — you just keep going while a condition holds:

let attempts = 0;
while (attempts < 3) {
  attempts = attempts + 1;
}

Be careful: if the condition in a while loop never becomes false, you've written an infinite loop, which will freeze whatever is running it. Always make sure something inside the loop moves you toward the exit condition.

Two keywords change a loop's flow mid-iteration:

  • break exits the loop entirely, right away.
  • continue skips the rest of the current iteration and jumps to the next one.
for (let i = 0; i < 10; i++) {
  if (i === 3) continue; // skip 3
  if (i === 6) break;    // stop entirely at 6
  console.log(i);
}
// logs 0, 1, 2, 4, 5

You'll also loop over arrays constantly — for (const item of someArray) reads naturally as "for each item of this array," and is usually clearer than tracking an index manually when you don't need the index itself.

Example

Summing numbers 1 through 5 with a for loop, then the same with a while loop.

let total = 0;
for (let i = 1; i <= 5; i++) {
  total += i;
}
console.log("for loop total:", total);

let n = 1;
let whileTotal = 0;
while (n <= 5) {
  whileTotal += n;
  n++;
}
console.log("while loop total:", whileTotal);

Try it yourself

Change the loop bound and see the total change.

Code editor. Press Escape then Tab to leave the editor if keyboard focus becomes trapped. Press Control+Shift+M inside the editor to toggle Tab-key focus trapping.

Loading editor…

Guided exercise

Guided exercise

Write a function `sumUpTo(n)` that returns the sum of all whole numbers from 1 up to and including n, using a for loop.

Checks: sumUpTo(5) is 15 · sumUpTo(1) is 1 · plus 1 hidden check

Code editor. Press Escape then Tab to leave the editor if keyboard focus becomes trapped. Press Control+Shift+M inside the editor to toggle Tab-key focus trapping.

Loading editor…

Stuck? Get a hint.

Independent exercise

Independent exercise

Write a function `countEvens(limit)` that returns how many even numbers exist from 1 up to and including limit, using continue to skip odd numbers.

Checks: countEvens(10) is 5 · countEvens(1) is 0 · plus 1 hidden check

Code editor. Press Escape then Tab to leave the editor if keyboard focus becomes trapped. Press Control+Shift+M inside the editor to toggle Tab-key focus trapping.

Loading editor…

Stuck? Get a hint.

Common mistakes

  • Writing a while loop whose condition never becomes false, freezing the program.
  • Off-by-one errors from using < instead of <= (or vice versa) in the loop condition.
  • Forgetting to update the loop variable, causing an infinite for loop even though it looks correct.

Knowledge check

Knowledge check

1. What are the three parts of a for loop's header?
2. What does `continue` do inside a loop?
3. What is most likely to cause an infinite loop?

Takeaway

for loops suit a known repeat count; while loops suit an unknown one — both need a clear path to stopping.

Summary

for loops combine an initializer, condition, and update in one line; while loops repeat purely based on a condition. break exits a loop entirely, and continue skips to the next iteration.

References

Your notes

Notes save automatically.

Finished this lesson?

Mark it complete to track your progress and schedule a future review.

Next: Functions