Break and Continue

The break statement immediately stops the loop or switch where it is used.
It exits the control structure before it finishes naturally.

πŸ“Œ Think of it as β€œI want to get out of this loop right now.”

for (let i = 1; i <= 5; i++) {
  if (i === 3) {
    break;
  }
  console.log(i);  // Output: 1, 2
}

➑️ As soon as i === 3, the loop stops completely. Nothing runs after that.


The continue statement skips the current iteration and moves to the next one in the loop.

πŸ“Œ Think of it as β€œSkip this round and continue with the next.”

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

➑️ When i === 3, console.log(i) is skipped, but the loop keeps running.


StatementCan use breakCan use continue
for loopβœ… Yesβœ… Yes
while loopβœ… Yesβœ… Yes
do...whileβœ… Yesβœ… Yes
switch caseβœ… Yes❌ No
if block❌ No❌ No

➑️ break works in both loops and switch
➑️ continue only works inside loops


  • The loop ends immediately
  • Code after the break is not executed
  • Control moves outside the loop

Example:

let i = 0;
while (i < 5) {
  if (i === 2) {
    break;
  }
  console.log(i);  // Output: 0, 1
  i++;
}

  • The loop skips the current iteration
  • Code after continue is ignored for that iteration
  • Control jumps to the next round of the loop

Example:

let i = 0;
while (i < 5) {
  i++;
  if (i === 3) {
    continue;
  }
  console.log(i);  // Output: 1, 2, 4, 5
}

In a for loop, break immediately exits the entire loop, even if the loop condition is still true.

🧠 Example:

for (let i = 1; i <= 5; i++) {
  if (i === 3) {
    break;
  }
  console.log(i);
}

πŸ“€ Output:

1
2

➑️ When i becomes 3, the break runs and the loop stops completely.


In a for loop, continue skips the current iteration and moves to the next one.

🧠 Example:

for (let i = 1; i <= 5; i++) {
  if (i === 3) {
    continue;
  }
  console.log(i);
}

πŸ“€ Output:

1
2
4
5

➑️ When i is 3, the loop skips console.log(i), but continues with 4 and 5.


Yes, absolutely.
That’s the most common way we use them β€” with if conditions inside loops.

🧠 Example using both:

for (let i = 1; i <= 5; i++) {
  if (i === 2) {
    continue;  // Skip when i is 2
  }
  if (i === 4) {
    break;     // Stop when i is 4
  }
  console.log(i);
}

πŸ“€ Output:

1
3

➑️ Here’s what happens:

  • i = 1 β†’ printed
  • i = 2 β†’ skipped
  • i = 3 β†’ printed
  • i = 4 β†’ loop stopped

βœ… Summary Table:

for Loop ValueActionOutput
1print1
2continueβ€”
3print3
4breakβ€” (loop ends)

Just like in a for loop, break in a while loop immediately exits the loop, no matter what the condition says.

🧠 Example:

let i = 1;
while (i <= 5) {
  if (i === 3) {
    break;
  }
  console.log(i);
  i++;
}

πŸ“€ Output:

1
2

➑️ When i === 3, the break runs. The loop stops even though the condition i <= 5 is still true.


continue skips the rest of the current iteration and goes back to check the condition again.

🧠 Example:

let i = 0;
while (i < 5) {
  i++;
  if (i === 3) {
    continue;
  }
  console.log(i);
}

πŸ“€ Output:

1
2
4
5

➑️ When i === 3, console.log(i) is skipped, and the loop moves to i = 4.

❗ Be careful:
You must increment i before the continue β€” or else it may create an infinite loop.


The do...while loop behaves mostly the same as while, but:

πŸ”Ή It runs at least once, even if the condition is false
πŸ”Ή break and continue still work as expected


🧠 Example with break:

let i = 1;
do {
  if (i === 3) {
    break;
  }
  console.log(i);
  i++;
} while (i <= 5);

πŸ“€ Output:

1
2

➑️ Stops when i === 3.


🧠 Example with continue:

let i = 0;
do {
  i++;
  if (i === 3) {
    continue;
  }
  console.log(i);
} while (i < 5);

πŸ“€ Output:

1
2
4
5

➑️ When i === 3, it skips printing, then goes to the next iteration.


Loop Typebreak works?continue works?Key Note
whileβœ… Yesβœ… YesAlways check loop condition first
do...whileβœ… Yesβœ… YesRuns at least once

You should use break when you want to stop a loop early β€” usually when a specific condition is met, and there’s no need to continue looping.

πŸ’‘ Real-life Examples:

  • Searching for a value in an array:
let products = ['pen', 'pencil', 'eraser', 'book'];
let searchItem = 'eraser';

for (let i = 0; i < products.length; i++) {
  if (products[i] === searchItem) {
    console.log('Item found at index:', i);
    break; // No need to continue after finding
  }
}
  • Input validation: Stop taking input as soon as an invalid value appears.

Use continue when you want to skip certain values, but still keep running the loop for others.

πŸ’‘ Real-life Examples:

  • Skipping odd numbers and printing only even ones:
for (let i = 1; i <= 10; i++) {
  if (i % 2 !== 0) {
    continue; // Skip odd numbers
  }
  console.log(i); // Prints even numbers only
}
  • Filtering bad data in a list (like null or undefined):
let scores = [90, null, 78, undefined, 100];

for (let i = 0; i < scores.length; i++) {
  if (scores[i] == null) {
    continue; // Skip bad entries
  }
  console.log('Valid score:', scores[i]);
}

Yes, in most cases, you can re-write your logic using if/else, but it may be less clean or less efficient.

Without break:

let products = ['pen', 'eraser', 'book'];
let found = false;

for (let i = 0; i < products.length; i++) {
  if (!found && products[i] === 'eraser') {
    console.log('Found!');
    found = true;
  }
}

Without continue:

for (let i = 1; i <= 5; i++) {
  if (i !== 3) {
    console.log(i);
  }
}

Because they:

  • Make your code shorter
  • Avoid unnecessary checks or iterations
  • Help make intent clear (like: β€œstop when found” or β€œskip bad data”)

Use them when they make your code cleaner and more readable.
But if they confuse the logic or make bugs more likely β€” avoid overusing.


βœ… 1. What happens if I use break outside of a loop or switch?

You get a SyntaxError ❌

break; // ❌ Error: Illegal break statement

πŸ” Why?
break only works inside:

  • for, while, do...while loops
  • switch statements
  • Or labeled blocks (advanced)

Yes βœ…

JavaScript allows labeled blocks, which let you break or continue outer loops.

πŸ’‘ Example β€” break from outer loop:

outerLoop:
for (let i = 1; i <= 3; i++) {
  for (let j = 1; j <= 3; j++) {
    if (j === 2) {
      break outerLoop;
    }
    console.log(`i=${i}, j=${j}`);
  }
}

πŸ“€ Output:

i=1, j=1

➑️ It exited both loops because of the labeled break.

πŸ’‘ continue with labels:

outerLoop:
for (let i = 1; i <= 3; i++) {
  for (let j = 1; j <= 3; j++) {
    if (j === 2) {
      continue outerLoop;
    }
    console.log(`i=${i}, j=${j}`);
  }
}

πŸ“€ Output:

i=1, j=1
i=2, j=1
i=3, j=1

➑️ It skips the inner loop and jumps to the next outer loop iteration.

⚠️ Labels are rare. Use them only when necessary, as they make code harder to read.


KeywordStops just the loop?Stops the entire function?
breakβœ… Yes❌ No
returnβœ… Yesβœ… Yes (and returns a value)

πŸ” Example with break:

function findFirstEven(numbers) {
  for (let num of numbers) {
    if (num % 2 === 0) {
      console.log("Even found!");
      break; // Loop ends, function continues
    }
  }
  console.log("Function still running...");
}

πŸ” Example with return:

function findFirstEven(numbers) {
  for (let num of numbers) {
    if (num % 2 === 0) {
      return "Even found"; // Entire function exits
    }
  }
  return "No even number";
}

Yes. That’s a common beginner mistake.
If you use continue before important logic, that code will be skipped.

❌ Bad Example:

for (let i = 1; i <= 5; i++) {
  if (i === 3) {
    continue;
  }
  let doubled = i * 2; // This line is skipped for i = 3
  console.log(doubled);
}

πŸ“€ Output:

2
4
8
10

➑️ 6 is missing because it was skipped at i === 3.


PitfallHow to Avoid
break outside loop/switch❌ Not allowed
Skipping logic with continueβœ… Place continue after important code if needed
Confusing return vs breakUse return to exit function, break to exit loop
Overusing labelsAvoid unless you really need to jump out of nested loops

  1. How would I write a loop that stops when a number is divisible by 7 using break?
  2. How can I skip even numbers in a loop using continue?
  3. Can I use both break and continue in the same loop?
  4. How do nested loops react to break and continue?

Thank You!

Leave a Comment

Your email address will not be published. Required fields are marked *