JavaScript Break and Continue Statements

In JavaScript, we often work with JavaScript loops (for loop/ while loop/ do-while loops). While programming, sometimes it is required to break the loop when a specified condition is met. Also, sometimes you may want to skip the loop iterations in the program. JavaScript offers two statements for this purpose, i.e., Break and Continue Statement. In event-driven programming, similar control flows are essential for handling asynchronous events effectively.
In JavaScript, we have control over switch statements and loops. We can either exit any loop immediately or start iteration of any loop with the help of Break and Continue statements, respectively. That’s why these statements are also called Loop Control Statements.
Codeleaks is one of the best platforms that offer important JavaScript tutorials in one place.
This article will teach you how to use JavaScript Break and Continue Statements to control the loops and program execution. There will be examples for better understanding.
Break Statement
We use the JavaScript Break statement to exit the loop immediately. We can also exit the switch statement with the break statement. After exiting the loop, the remaining program after the loop is executed. It passes the control to the statement after loop with the help of the Break Statement. It was first introduced for Switch Case statements, but afterward, it is also used for loops. Switch Case statements works as flow control statements.
Example:
<!DOCTYPE html>
<html>
<head>
<title>
Break statement in JavaScript
</title>
</head>
<body>
<h2>
While Loop
</h2>
<script>
let x=1;
while(x<20)
{
++x;
if((x%10==0))
{
break;
}
else
{
document.write(“Loop for ” +x+ “is running”)
document.write(“<BR/>”)
}
}
document.write(“BREAK STATEMENT exits the Loop.”);
</script>
</body>
</html>
Output:

Explanation:
In the above code, we made one condition in the while loop to break the loop. If the value of variable x becomes divisible by 10, JavaScript Break Statement will stop the loop execution.
Continue Statement

