JavaScript

JavaScript is a high-level, interpreted programming language that is widely used for web development. Initially designed as a client-side scripting language, it runs directly in web browsers, enabling dynamic and interactive user experiences. JavaScript can now be used for server-side development as well.

JavaScript Break and Continue

8 April 2025 | Category:

JavaScript provides two keywords to control loop execution:

  • 🔴 break → Exits the loop completely.
  • 🔁 continue → Skips the current iteration and jumps to the next one.

🧨 JavaScript break Statement

The break statement stops the loop immediately, even if the condition is still true.

✅ Syntax

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

Output:

0
1
2
3
4

📝 As soon as i becomes 5, the loop stops.


🔁 JavaScript continue Statement

The continue statement skips the current loop iteration, and continues with the next one.

✅ Syntax

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

Output:

0
1
2
3
4
6
7
8
9

📝 When i is 5, the loop skips console.log(i) and moves to the next iteration.


🔄 Using with while Loop

Both break and continue work the same way inside a while loop.

Example with break:

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

Example with continue:

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

💡 When to Use

StatementUse Case Example
breakStop the loop early (e.g., on condition)
continueSkip specific values (e.g., filter values)

✅ Real-World Example: Stop at a Target Value

let numbers = [10, 20, 30, 40, 50];

for (let num of numbers) {
  if (num === 30) {
    break;
  }
  console.log(num);
}

Output:

10
20

🧾 Summary

KeywordBehavior
breakExits the loop completely
continueSkips current iteration, continues