Break, Continue and Labels

Updated · 6 min read
easyES2026javascriptloopsbreakcontinuelabels

break ends a loop immediately, and the program continues after it. continue skips the rest of the current round and moves on to the next one. Together they let a loop stop as soon as it has found what it was looking for, or skip items that don't apply, without wrapping the whole body in an if. Labels extend both to nested loops.

In One Minute

  • break leaves the innermost loop (or switch) right away.
  • continue jumps to the next round; in a for loop, the update still runs.
  • A label (outer:) before a loop lets break outer or continue outer act on that outer loop.
  • In a function, return often reads better than break, and array methods such as find and some replace many search loops.

break: Stop the Loop

Searching is the classic use: once the item is found, there is no reason to look at the rest.

break.js▶ Try it
const payments = [
  { name: "Navika", amount: 120 },
  { name: "Srihitha", amount: 0 },
  { name: "Advitha", amount: 95 },
];
 
let firstUnpaid = null;
for (const payment of payments) {
  console.log("Checking", payment.name);
  if (payment.amount === 0) {
    firstUnpaid = payment.name;
    break;
  }
}
console.log("First unpaid:", firstUnpaid);
Output
Checking Navika
Checking Srihitha
First unpaid: Srihitha

Explanation: the loop stops at Srihitha; Advitha is never checked. The loop here is for...of, which gives each item in turn; it has its own lesson next.

break works in for, for...of, for...in, while and do...while loops, and in switch. It does not work in forEach callbacks, because the callback is a separate function, not a loop body.

continue: Skip One Round

continue skips to the next round. It is useful to filter out items early, keeping the main code unindented:

continue.js▶ Try it
const fees = [120, 0, 85, -10, 45];
let total = 0;
 
for (const fee of fees) {
  if (fee <= 0) {
    console.log("Skipping", fee);
    continue;
  }
  total += fee;
}
console.log("Total:", total);
Output
Skipping 0
Skipping -10
Total: 250

In a classic for loop, continue still runs the update part (i++). In a while loop, it jumps straight to the condition, so make sure the variable that controls the loop is updated before the continue:

while-continue.js▶ Try it
let i = 0;
while (i < 5) {
  i++;                     // update first
  if (i % 2 === 0) continue;
  console.log("Odd:", i);
}
Output
Odd: 1
Odd: 3
Odd: 5

Labels for Nested Loops

break and continue act on the innermost loop. To act on an outer loop, put a label (a name followed by a colon) in front of it:

labels.js▶ Try it
const classes = [
  ["Navika", "Srihitha"],
  ["Advitha", "Navika"],
];
const wanted = "Advitha";
 
search: for (let c = 0; c < classes.length; c++) {
  for (let s = 0; s < classes[c].length; s++) {
    if (classes[c][s] === wanted) {
      console.log(`Found ${wanted} in class ${c + 1}, seat ${s + 1}`);
      break search;
    }
  }
}
console.log("Search finished");
Output
Found Advitha in class 2, seat 1
Search finished

Explanation: a plain break would only end the inner loop, and the outer loop would keep going. break search ends both. continue search would skip to the next round of the outer loop.

Labels are rare in everyday code. When you find yourself reaching for one, consider moving the nested loops into a function and using return:

return.js▶ Try it
function findSeat(classes, wanted) {
  for (let c = 0; c < classes.length; c++) {
    for (let s = 0; s < classes[c].length; s++) {
      if (classes[c][s] === wanted) return { classNumber: c + 1, seat: s + 1 };
    }
  }
  return null;
}
 
console.log(findSeat([["Navika"], ["Srihitha", "Advitha"]], "Advitha"));
console.log(findSeat([["Navika"]], "Advitha"));
Output
{ classNumber: 2, seat: 2 }
null

Array Methods That Stop Early

Many break loops are searches, and arrays have methods for exactly that. They stop at the first match, like break:

Loop with break that...Array method
finds the first matching itemlist.find(test)
finds its indexlist.findIndex(test)
checks if any item matcheslist.some(test)
checks if every item matcheslist.every(test)
methods.js▶ Try it
const amounts = [120, 0, 95];
console.log(amounts.find((a) => a === 0));
console.log(amounts.some((a) => a === 0));
console.log(amounts.every((a) => a >= 0));
Output
0
true
true

They are covered fully in the array search lesson.

In Practice

The Fee Tracker processes a list of payment records. It skips blank lines with continue, and stops with break at an END marker, ignoring anything after it:

process.js▶ Try it
const lines = ["Navika,120", "", "Srihitha,85", "END", "Advitha,999"];
let total = 0;
let count = 0;
 
for (const line of lines) {
  if (line === "") continue;
  if (line === "END") break;
 
  const [name, amount] = line.split(",");
  total += Number(amount);
  count++;
  console.log(`Recorded ${name}: $${amount}`);
}
console.log(`${count} payments, $${total}`);
Output
Recorded Navika: $120
Recorded Srihitha: $85
2 payments, $205

Common Mistakes

Using break inside forEach. It is a SyntaxError ("Illegal break statement"). Use a for...of loop, or some/find, which stop early.

continue before the update in a while loop. The update never runs, and the loop never ends.

Expecting break to exit all nested loops. It exits one. Use a label, or move the loops into a function and return.

Too many continue statements. Several skips scattered through a long body make the flow hard to follow. Filter the data first (list.filter(...)) and loop over the result.

Practice

Challenge: Loop over the numbers 1 to 20. Skip multiples of 3 with continue, stop the loop entirely when you reach the first number greater than 10 that is a multiple of 7, and print the numbers you kept.

Show solution
practice.js▶ Try it
const kept = [];
for (let n = 1; n <= 20; n++) {
  if (n % 3 === 0) continue;
  if (n > 10 && n % 7 === 0) break;
  kept.push(n);
}
console.log(kept);
Output
[1, 2, 4, 5, 7, 8, 10, 11, 13]

Quick check: Break and continue

3 questions
1What does continue do in a for loop?
2Which statement exits two nested loops at once?
3Why can't you use break inside forEach?

Key Takeaways

KeywordEffect
breakLeave the loop now
continueGo to the next round
break labelLeave the labeled outer loop
returnLeave the whole function (and every loop in it)

Was this tutorial helpful?

Lesson 17 of 139