SKU // 26

JavaScript Break

break "jumps out" of a loop or a switch immediately, even if the loop's condition is still true. Use it when you have found what you were looking for and there is no reason to keep going.

// break in Loops

When JavaScript reaches break inside a loop, the loop ends on the spot. No more iterations run, and the program continues with the first statement after the loop:

for (let i = 1; i <= 10; i++) {
  if (i === 4) {
    break;
  }
  console.log(i);
}
console.log("Loop finished");
1 2 3 Loop finished

When i reaches 4, break fires before the console.log, so 4 is never printed, and neither is anything after it. break almost always sits inside an if: "if this happens, stop".

// Stopping a Search Early

The most common use: search a list and stop as soon as you find a match.

const names = ["Riley", "Jordan", "Sam", "Casey", "Drew"];
let foundAt = -1;

for (let i = 0; i < names.length; i++) {
  console.log("Checking " + names[i]);
  if (names[i] === "Sam") {
    foundAt = i;
    break;
  }
}
console.log("Found at position " + foundAt);
Checking Riley Checking Jordan Checking Sam Found at position 2

Casey and Drew were never checked. On a list of a million items, stopping early can save a lot of work.

Tip
Setting foundAt to -1 before the loop is a common pattern. If the loop finishes without a match, -1 signals "not found", since no real position is negative.

// Escaping a Deliberate Endless Loop

Sometimes a loop is written with while (true) on purpose, and break is the only way out:

let attempts = 0;

while (true) {
  attempts++;
  const roll = Math.floor(Math.random() * 20) + 1;
  if (roll === 20) {
    console.log("Natural 20 on attempt " + attempts);
    break;
  }
}
Natural 20 on attempt 13 (varies every run)
Watch out
A while (true) loop without a reachable break freezes the page. Make sure the break condition can actually happen: roll === 21 on a 20-sided die never would.

// break in switch

You met break in the Switch lesson. It does the same job there: it exits the switch block after a matching case runs. Without it, execution "falls through" into the following cases, even though their values do not match.

switch (dayNum) {
  case 0:
    day = "Sunday";
    break;       // leave the switch here
  case 1:
    day = "Monday";
    break;
  // ...
}
Watch out: break in a switch inside a loop
If a switch sits inside a loop, a break in one of its cases exits only the switch, not the loop. The loop carries on with its next pass. To leave the loop from inside the switch, use a label (below).

// break Only Exits One Loop

In nested loops, break exits only the innermost loop it sits in. The outer loop keeps going:

for (let row = 1; row <= 3; row++) {
  for (let col = 1; col <= 3; col++) {
    if (col === 2) break;
    console.log(row + "," + col);
  }
}
1,1 2,1 3,1

// Labels

A label gives a name to a statement or block, so break (and continue) can refer to it. A label is a name followed by a colon, placed right before the statement:

labelname: statement;

labelname: {
  statements
}

With a label, break labelname; exits the labeled statement instead of just the innermost loop. Compare these two, which differ only in which loop the break targets:

let text = "";
loop1: for (let j = 1; j < 5; j++) {
  loop2: for (let i = 1; i < 5; i++) {
    if (i === 3) { break loop1; }    // exit BOTH loops
    text += i;
  }
}
console.log(text);
12
let text = "";
loop1: for (let j = 1; j < 5; j++) {
  loop2: for (let i = 1; i < 5; i++) {
    if (i === 3) { break loop2; }    // exit the inner loop only
    text += i;
  }
}
console.log(text);
12121212

In the first, hitting i === 3 ends everything after one pass: "12". In the second, it ends only the inner loop, so the outer loop runs all 4 times, adding "12" each time. (break loop2 does the same as a plain break here, since loop2 is the innermost loop.)

Breaking out of a block

Without a label, break only works inside a loop or switch. With a label, it can jump out of any code block. break and continue are the only statements that can jump out of a block like this:

const cars = ["BMW", "Volvo", "Saab", "Ford"];
let text = "";

list: {
  text += cars[0] + " ";
  text += cars[1] + " ";
  break list;                 // skip the rest of the block
  text += cars[2] + " ";
  text += cars[3] + " ";
}
console.log(text);
BMW Volvo
Note
Labels are rare in everyday code, and most programmers never use a labeled block. It is worth recognizing them, though, because loop1: in front of a loop looks confusing if you have never seen one. If you find yourself needing a label, the nested loop can often go into its own function instead, where return exits everything at once (see JS Control Flow).

// Quick Check

What does this log?

for (let i = 0; i < 10; i++) {
  if (i * i > 10) break;
  console.log(i);
}
Show answer
0, 1, 2, 3. At i = 4, 16 > 10 is true, so the loop stops before printing 4.

A break sits inside an inner loop, which is inside an outer loop. No labels are used. What happens when it runs?

  1. Both loops stop
  2. Only the inner loop stops; the outer loop continues
  3. Only the outer loop stops
  4. The whole program stops
Show answer
B. A plain break exits only the innermost loop it is in. To stop the outer loop too, label it and use break outerLabel;.

Where can a plain break (no label) be used?

  1. Anywhere in a program
  2. Inside an if block only
  3. Inside a loop or a switch
  4. Inside a function only
Show answer
C. A plain break must be inside a loop or switch. It is often written inside an if, but that if itself has to be inside a loop or switch. Anywhere else, it is a SyntaxError.

// Try It

  1. Click Open Playground. Loop from 1 to 100 and break at the first number divisible by both 7 and 5. Log that number
  2. Search an array of numbers for the first negative value; print its position, or "none found" if the result is still -1
  3. Write a while (true) loop that breaks after five passes. Write the break before you click Run
  4. Open the break loop1 example with its Try it Yourself button and confirm you get 12. Change it to break loop2 and confirm 12121212. Explain the difference in your notes
// Video Walkthroughs
Last updated: Oct 9, 2026, 11:58 PM ET