Break and Continue in JavaScript

Introduction

break exits a loop or switch early. continue skips the rest of the current iteration and moves to the next one. They help you avoid extra nesting and express “stop” or “skip” clearly inside loops and switch cases.

Prerequisites

break in Loops

javascript
// Find first score >= 90
const scores = [72, 85, 91, 88, 95];
let firstHigh = null;
 
for (const score of scores) {
  if (score >= 90) {
    firstHigh = score;
    break;
  }
}
 
console.log("First high score:", firstHigh);

Without break, the loop would keep scanning after finding a match.

continue in Loops

javascript
// Sum only positive numbers, skip others
const values = [10, -3, 5, 0, 7, -1];
let sum = 0;
 
for (const value of values) {
  if (value <= 0) {
    continue;
  }
  sum += value;
}
 
console.log("Positive sum:", sum);

break in switch

Already required to avoid fall-through:

javascript
// break prevents fall-through
const role = "guest";
 
switch (role) {
  case "admin":
    console.log("Full access");
    break;
  case "editor":
    console.log("Edit access");
    break;
  default:
    console.log("Read only");
}

Labeled break (Rare)

For nested loops, labels can target an outer loop—use sparingly:

javascript
// Labeled break exits outer loop
outer: for (let row = 0; row < 3; row++) {
  for (let col = 0; col < 3; col++) {
    if (row === 1 && col === 1) {
      break outer;
    }
    console.log(`(${row}, ${col})`);
  }
}

Prefer smaller functions or flags when possible—labeled breaks are uncommon in application code.

continue in while

javascript
// Skip empty names
const names = ["Ada", "", "Lin", null, "Sam"];
let i = 0;
 
while (i < names.length) {
  const name = names[i];
  i += 1;
  if (!name) {
    continue;
  }
  console.log(`Hello, ${name}`);
}

Avoid Overusing break / continue

Sometimes a clear if block reads better than continue:

javascript
// Readable if instead of continue
for (const item of items) {
  if (item.active) {
    process(item);
  }
}

Choose the style your team finds clearest.

Mini Example: Search with Early Exit

javascript
// Find user by id
const users = [
  { id: 1, name: "Ada" },
  { id: 2, name: "Lin" },
  { id: 3, name: "Sam" },
];
 
const targetId = 2;
let found = null;
 
for (const user of users) {
  if (user.id === targetId) {
    found = user;
    break;
  }
}
 
console.log(found ?? "User not found");

FAQ

Does break exit only the innermost loop?

Yes, unless you use a label on an outer loop.

Can continue skip to the next while iteration?

Yes—it jumps to the condition check after continue.

Is return the same as break?

return exits the entire function, not just the loop.

What comes next?

Objects overview and object creation chapters.