Skip to content
Tutorials Center
My learning

Topics

Lesson 27 of 156 ยท Control Flow

break, continue and Labels

Control loops precisely with break and continue in JavaScript. Exit early, skip items, and use labels to break out of nested loops cleanly.

Loops normally run until their condition fails. But real problems often call for a change of plan halfway through. If you're searching a list and find what you want on the third item, there's no reason to check the other thousand. If an item is invalid, you might want to skip it and move on to the next one.

JavaScript gives you two statements for this: break leaves a loop immediately, and continue skips the rest of the current iteration and jumps to the next one. For nested loops, labels let you say which loop you mean. This lesson covers all three, along with the cases where you're better off without them.

break: leave the loop now

When JavaScript reaches break inside a loop, the loop ends on the spot. No more iterations run, and the update step and condition aren't checked again. Execution continues with the first statement after the loop.

JS Stopping a search early

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The log shows only two checks. Without break, the loop would keep checking, and worse, it would overwrite firstPending with order 104, the last pending order.

break works the same way in for, while, do...while, for...of and for...in loops. It's also what ends a case in a switch statement.

break with while (true)

In the previous lesson you saw while (true), a loop that only ends from the inside. break is how it ends. This shape fits when the exit check naturally lands in the middle of the body:

JS Exit in the middle

Press Run (or Ctrl + Enter) to see the console output here.

No output.

That example also sneaks in continue, which is next.

continue: skip to the next iteration

continue abandons the current iteration and jumps ahead to the next one. The loop itself keeps running. In a for loop, the update step (i++) still runs before the condition is checked again. In a while loop, JavaScript goes straight back to the condition.

JS Skipping items

Press Run (or Ctrl + Enter) to see the console output here.

No output.

continue works like a guard clause for a loop body: handle the cases you want to skip at the top, and the main logic stays unindented below. Compare it with the alternative of wrapping the whole body in if (isValid) { โ€ฆ }.

continue in a while loop can skip your progress step. If the counter is updated at the bottom of the body, continue jumps over it, and the loop may never end:

JavaScript
let i = 0;
while (i < 5) {
  if (i === 2) continue; // i stays 2 forever!
  console.log(i);
  i++;
}

Move the update to the top of the body, or use a for loop, whose update step always runs.

Labels: choosing which loop

In nested loops, break and continue affect only the innermost loop they're in. Sometimes that's not what you want. Say you're searching a grid and find the target in the inner loop. break stops the inner loop, but the outer loop carries on with the next row:

JS break only exits the inner loop

Press Run (or Ctrl + Enter) to see the console output here.

No output.

A label fixes this. A label is a name followed by a colon, placed right before a loop. Then break name or continue name targets that specific loop:

JS A labeled break

Press Run (or Ctrl + Enter) to see the console output here.

No output.

break search ends the loop labeled search, which is the outer one. continue rows jumps to the next iteration of the outer loop, skipping both the rest of the inner loop and the console.log after it.

Label names follow the same rules as variable names. They don't clash with variables, and they only exist on the statement they label. You can't break to a label that isn't wrapped around your current position.

Use labels sparingly

Labels are a legitimate part of JavaScript, but they're rare in everyday code, and many readers will pause when they see one. Often there's a clearer alternative: move the nested loops into a function and use return, which leaves every loop at once.

JavaScript
function findInGrid(grid, target) {
  for (let row = 0; row < grid.length; row++) {
    for (let col = 0; col < grid[row].length; col++) {
      if (grid[row][col] === target) return { row, col };
    }
  }
  return null;
}

The function has a name that explains what it does, and the result is a value you can reuse. You'll learn more in Return Values.

Keep loops readable

break and continue are useful, but every one of them is a hidden exit or jump that a reader has to notice. A few guidelines keep them under control:

  • Prefer one clear exit. If a loop has three different break statements scattered through a long body, it's hard to know why it stopped. Consider moving the loop into a function and returning a result instead.
  • Put continue checks at the top. Skips near the start of the body read like a filter: "ignore these, handle the rest". A continue buried halfway down is easy to miss.
  • Let the condition do the work when it can. while (!found && i < items.length) states the stopping rule up front, where readers look for it first.

break and continue vs array methods

Array methods such as forEach take a function to run for each item. break and continue don't work inside that function, because it isn't a loop body. Writing break there is a SyntaxError. A return inside the callback acts like continue (it ends that one call), but there is no way to stop forEach early.

When you need to stop early, use a real loop, or a method designed for it:

  • array.find(fn) stops at the first item for which fn returns a truthy value and gives you that item.
  • array.some(fn) stops as soon as one item passes and returns true.
  • array.every(fn) stops as soon as one item fails and returns false.

You'll meet these in Searching Arrays. For instance, the first example in this lesson collapses to one line: orders.find(o => o.status === "pending").

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What happens when a for loop reaches continue?

    continue skips the remaining statements of this iteration. In a for loop, the update step still runs before the condition is checked again.
  2. 2. In two nested loops, which loop does a plain break in the inner loop end?

    An unlabeled break exits the innermost enclosing loop. Use a label, or return from a function, to leave both.
  3. 3. What does outer: for (โ€ฆ) { for (โ€ฆ) { break outer; } } do?

    break outer ends the loop labeled outer, and since the inner loop is inside it, both stop.
  4. 4. Why can this loop never end? let i = 0; while (i < 5) { if (i === 2) continue; i++; }

    In a while loop, continue jumps straight back to the condition. The i++ at the bottom never runs once i is 2.
  5. 5. You need to stop as soon as you find a matching item in an array. Which choice will NOT work?

    break isn't allowed inside a callback function; it's a SyntaxError. forEach always visits every item.

Summary

  • break exits the innermost loop (or switch) immediately.
  • continue skips the rest of the current iteration; in a for loop the update still runs.
  • continue in a while loop can skip your counter update and cause an infinite loop.
  • A label (name: before a loop) lets break name and continue name target an outer loop.
  • Moving nested loops into a function and using return is often clearer than a label.
  • break and continue don't work in forEach callbacks; use a loop, or find, some and every to stop early.

Next: for...of and for...in โ€” simpler loops for walking through values and keys.