JavaScript Break
Break statement is used to break the immediate loop or switch statement, and carry on with the execution of next statements in the script.
The syntax of break statement is
JavaScript break statement can be used inside the following statements.
- switch
- for
- for in
- for of
- while
- do while
Usually, break statement is used conditionally inside a loop statement.
We will go through examples with break statement for each of these statements.
Break Switch Statement
In the following example, we use break statement at the end of each case block in switch statement.
index.html
Break For Loop
In the following example, we use break statement to exit For loop when i
becomes 4.
index.html
Break For-in Loop
In the following example, we use break statement to exit For-in loop when the value corresponding to any key is 22
.
index.html
Break For-of Loop
In the following example, we use break statement to exit For-of loop when the value of item is orange
.
index.html
Break While Loop
In the following example, we use break statement to exit While loop when i == 5
.
index.html
Break Do-While Loop
In the following example, we use break statement to exit Do-While loop when i == 5
.
index.html
Conclusion
In this JavaScript Tutorial, we learned what break statement is in JavaScript, its syntax, and usage with examples.