Loop Control: break, continue0%

Loop Control: break, continue

Beginner12 min read•Updated: Jul 11, 2026
Study Materials

Loop Control: break and continue

When executing loops, standard sequential execution isn't always enough. You may want to exit a loop early when a searched item is found, or skip the current iteration when encountering invalid data. JavaScript provides break and continue for granular loop control.


1. The break Statement: Emergency Exit

The break statement immediately terminates the entire loop and jumps execution to the first statement following the loop:

Output
Iteration 1 ---> Iteration 2 ---> [ break encountered! ]
|
v
[ Jump OUT of loop completely ]
JavaScript
const studentIds = [101, 204, 305, 409, 512, 630];
const target = 305;
let foundIndex = -1;
 
for (let i = 0; i < studentIds.length; i++) {
if (studentIds[i] === target) {
foundIndex = i;
console.log(`Target ${target} found at index ${i}!`);
break; // Stop looping! No need to check the remaining elements!
}
}

Without break, the loop would uselessly continue checking the remaining elements, wasting CPU cycles!


2. The continue Statement: Skip to Next Iteration

The continue statement does not terminate the loop. Instead, it skips the rest of the current iteration and jumps immediately to the next iteration (evaluating the increment and condition):

Output
Iteration 1 ---> Iteration 2 [ continue! ] ---> Iteration 3
|
+--> (Skips rest of body, proceeds to next cycle!)

Real-World Example: Filtering Bad Data

JavaScript
const transactions = [100, -20, 500, 0, -50, 300];
let totalDeposits = 0;
 
for (let i = 0; i < transactions.length; i++) {
// Skip withdrawals or zero amounts:
if (transactions[i] <= 0) {
continue; // Skip directly to next transaction!
}
 
totalDeposits += transactions[i];
}
 
console.log(`Total valid deposits: ₹${totalDeposits}`); // ₹900

3. Summary: break vs continue

StatementWhat it doesLoop Status
breakTerminates loop immediatelyExits the loop entirely
continueSkips remainder of current stepContinues with next step

4. Labeled Statements (Advanced)

When working with nested loops, a standard break only exits the innermost loop. To break out of an outer loop from within an inner loop, use a label:

JavaScript
outerLoop: for (let row = 0; row < 3; row++) {
for (let col = 0; col < 3; col++) {
if (row === 1 && col === 1) {
console.log("Treasure found! Breaking outer loop.");
break outerLoop; // Exits BOTH loops!
}
console.log(`Scanning cell (${row}, ${col})`);
}
}

Interactive Quiz

Practice Quiz

Question 1
What is the effect of encountering a break statement inside a loop?
Question 2
What is the effect of encountering a continue statement inside a loop?
Question 3
In a loop from 1 to 10, what numbers are printed by:
JavaScriptCode Snippet
for (let i = 1; i <= 5; i++) {
if (i === 3) continue;
console.log(i);
}
Question 4
When a break is executed inside an inner loop of a nested loop structure without labels, what happens?
Question 5
Can break be used inside a switch statement?
Next Lesson

String Basics

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
While and Do-While LoopsString Basics

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

PrevNext