Signup/Sign In

JavaScript break and continue Statements

JavaScript break and continue statements are known as Loop Control Statements as they are used to control the loops. These statements let you control every loop and switch statements in JavaScript code by enabling you to either break out of the loop completely or directly jump to the next iteration skipping the current ongoing loop iteration.

We can come out of any loop's execution, whenever required, by using the break statement and we can also skip the current iteration and start next iteration by using the continue statement.

JavaScript break Statement

You may recall that the break statement was introduced in switch case, this statement can also be used inside other loops like for loop and while/do-while loops.

Let us recall the break statement once again, the break statement lets us break out of the loop execution or any switch statement and move on to execute the code after the end of the loop or switch.

Here is a simple example,

In the above example, when the value of the variable count becomes divisible by 5, we break out of the loop, and hence the loop execution stops. Try removing the break statement and you will see the loop will get executed 10 times.

JavaScript continue Statement

The continue statement can be used to skip the current loop iteration and jump to the next iteration if the loop condition is satisfied. This is used in case, for some specific condition in the loop execution you want to perform no action and directly jump to next iteration then the continue statement can be used.

Let's take an example,

In the code example above, whenever the value of the variable count is divisible by 2, we do nothing and call the continue statement to directly jump to the next iteration of the loop. Hence you can see that the document.write code is executed for only those iterations where the value of the count variable was not divisible by 2, which means for odd values only.

JavaScript Labels

By using the JavaScript labels you can control the flow of your code's execution more precisely. Where the break and continue statements can only be used to break or skip iteration of a code block in a loop or switch case, using a label with break and continue statement lets you control the execution of any code block.

To use JavaScript labels, you must first define a label, which is nothing but a name followed by a colon(:) and then your code.

Then the JavaScript label can be used by writing break or continue followed by the label_name.

Let's take an example to understand this.

As you can see in the example above, we have specified a label with name EXECUTE and then used it along with the break statement to specify which code block to break out of.

In this tutorial, we learned about the break and continue statements which are very important statements when it comes to controlling the flow of execution of the code within a loop or outside a loop.



About the author:
I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development. Founder @ Studytonight