Difference between Break and Continue Statement in JavaScript
Hii Everyone, I am Vaishnavi Mehrotra. I joined The Hacking School coding Bootcamp. The atmosphere is very good, My instructor Prashant is a very awesome and very excellent teacher. The hacking school coding Bootcamp is the best place to enhance your skills in Coding. I found myself very lucky to be part of this institution.
Break
The break statement is used to break the iteration or the discontinuation of the loop. If the defined condition is true, the break statement is executed in the loop or switch.
A break statement is used in various loops like for, while, do-while, foreach loop, and the switch case statement.
Syntax:
if (condition)
{
break;
}
Example:
for (var i = 1; i <= 5; i++) {
if (i === 3) {
console.log("Three");
break;
} else {
console.log(i);
}
}
Continue
The continue statements are used only to skip the iteration and move to the next iteration of the loop. If the defined condition is true, the continue statement is executed in the loop or switch.
A continue statement is also used in various loops like for, while, do-while, foreach loop, and the switch case statement.
Syntax:
if (condition)
{
continue;
}
Example:
for (var i = 1; i <= 5; i++) {
if (i === 3) {
continue;
} else {
console.log(i);
}
}
Summary
- The break statement will completely terminate the loop.
- The continue statement will skip the current iteration only.