Break and Continue
Learn how to control loop execution using break and continue in JavaScript.
Break and Continue
Loops run from start to finish — but sometimes you need to step in and change that. Maybe you found what you were looking for and want to stop early. Or maybe you want to skip a specific item and move on to the next one.
JavaScript gives you two keywords for this:
break— stop the loop completely and exit itcontinue— skip the current iteration and move to the next one
break — Exit the Loop Early
break immediately stops the loop and jumps to the code after it. Nothing else in the loop runs.
for (let i = 1; i <= 10; i++) {
if (i === 5) {
break;
}
console.log(i);
}
// 1
// 2
// 3
// 4The loop was set to run 10 times. But when i hit 5, break fired and the loop stopped. 5 itself never printed because break ran before console.log.
How break Works
Real Use of break — Searching
The most common use of break is stopping a search once you find what you need. There is no point continuing after the item is found.
const users = ["Sara", "Ali", "Zara", "Omar", "Hassan"];
const target = "Zara";
let found = false;
for (let i = 0; i < users.length; i++) {
if (users[i] === target) {
found = true;
console.log(`Found "${target}" at index ${i}`);
break; // no need to keep looking
}
}
if (!found) {
console.log(`"${target}" not in list.`);
}
// Found "Zara" at index 2Without break, the loop would keep running through Omar and Hassan even after finding Zara. That is wasted work — break makes it efficient.
continue — Skip to the Next Iteration
continue skips the rest of the current iteration and jumps straight to the next one. The loop does not stop — it just skips one cycle.
for (let i = 1; i <= 10; i++) {
if (i % 2 === 0) {
continue; // skip even numbers
}
console.log(i);
}
// 1
// 3
// 5
// 7
// 9Every time i is even, continue fires — skipping console.log for that iteration. Odd numbers print normally.
How continue Works
Real Use of continue — Filtering
continue is great when you want to process most items but skip specific ones.
const products = [
{ name: "Laptop", inStock: true },
{ name: "Mouse", inStock: false },
{ name: "Keyboard", inStock: true },
{ name: "Monitor", inStock: false },
{ name: "Webcam", inStock: true },
];
for (const product of products) {
if (!product.inStock) {
continue; // skip out of stock items
}
console.log(`Available: ${product.name}`);
}
// Available: Laptop
// Available: Keyboard
// Available: WebcamInstead of wrapping all your logic in an if block, continue lets you filter out unwanted items early and keep the main logic clean and unindented.
break and continue in while Loops
Both keywords work in every kind of loop — not just for loops.
// break in while
let num = 0;
while (true) { // intentionally infinite
num++;
if (num === 5) {
break; // only way out
}
}
console.log(num); // 5while (true) creates an intentionally infinite loop. break is the planned exit. This pattern is actually common in real code — keep running until a specific condition is met, then break out cleanly.
// continue in while
let i = 0;
while (i < 10) {
i++;
if (i % 3 === 0) {
continue; // skip multiples of 3
}
console.log(i);
}
// 1
// 2
// 4
// 5
// 7
// 8
// 10Be careful with continue in a while loop. Make sure your update (i++) happens before the continue — otherwise you skip the increment and create an infinite loop.
Using Both Together
Sometimes you need both in the same loop — skip some items, but stop entirely if a certain condition is reached.
const orders = [
{ id: 1, status: "delivered" },
{ id: 2, status: "pending" },
{ id: 3, status: "cancelled" },
{ id: 4, status: "pending" },
{ id: 5, status: "fraud" },
{ id: 6, status: "pending" },
];
for (const order of orders) {
if (order.status === "cancelled") {
continue; // skip cancelled orders
}
if (order.status === "fraud") {
console.log(`Fraud detected on order ${order.id}. Stopping.`);
break; // stop everything
}
console.log(`Processing order ${order.id}`);
}
// Processing order 1
// Processing order 2
// Processing order 4
// Fraud detected on order 5. Stopping.Cancelled orders are quietly skipped. A fraud order stops processing entirely. Order 6 never gets processed because break fired on order 5.
Summary
breakexits the loop immediately — nothing after it in the loop runscontinueskips the current iteration and moves to the next one- Both work in
for,while, anddo...whileloops - Use
breakwhen you have found what you need and there is no reason to keep going - Use
continuewhen you want to skip specific items but keep looping - In
whileloops, make sure your update runs beforecontinueto avoid infinite loops