JavaScript break / continue

Beginner
5 min read
Updated: Nov 2026
3 examples

Definition and Usage

break leaves the nearest loop or switch. continue skips the rest of the current loop iteration and goes to the next one.

JavaScript Syntax

JavaScript
for (let i = 0; i < 5; i++) {
  if (i === 1) continue;
  if (i === 3) break;
  console.log(i);
}
Try it Editor

How It Works

  1. continue at 1. Skip printing 1.
  2. break at 3. Stop before printing 3 or 4.

Examples

Example 1 — break in while

Stop when a condition appears mid-body.

JavaScript
let i = 0;
while (true) {
  if (i === 2) break;
  console.log(i);
  i++;
}
Try it Editor

How It Works

  1. Infinite while until break.
  2. Stops when i is 2.

Example 2 — continue skips

Jump to the next iteration.

JavaScript
for (const n of [1, 2, 3, 4]) {
  if (n % 2 === 0) continue;
  console.log(n);
}
Try it Editor

How It Works

  1. Even numbers are skipped.
  2. Odds print.

Example 3 — break in switch

Prevents fall-through.

JavaScript
switch (1) {
  case 1: console.log("one"); break;
  case 2: console.log("two"); break;
}
Try it Editor

How It Works

  1. break ends the switch after case 1.

Frequently Asked Questions

No. continue is for loops. switch uses break.
Labels can break an outer loop. Prefer simpler structure when you can.

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful