JavaScript Break Statement

The JavaScript Break Statement is an important keyword used to alter the flow of a program. Loops are used to execute a certain block of statements for n number of times until the test condition is false. There will be some situations where we have to terminate the loop without executing all the statements. In these situations, we can use JavaScript Break statement and Continue statement.

The JavaScript Break Statement is very useful to exit from any loop such as JavaScript For Loop, While Loop, and Do While Loop. While executing these loops, if the JavaScript compiler finds the break statement inside them, the loop will stop running the statements and immediately exit from the loop.

For example, we have five statements inside the loop, and we want to exit from the loop when a certain condition is True; otherwise, it has to execute all of them. In these situations, we can place the JavaScript Break statement inside the If condition. If the condition is True, then the compiler will execute the break statement. It means the JavaScript break statement will completely exit the controller from the loop. Otherwise, it will execute all the statements.

NOTE: Break statement in JavaScript is the most important statement in Switch Case. Without using Break, the compiler won’t exit from the Switch Case

JavaScript Break Statement Examples

The syntax of the JavaScript Break Statement is as follows:

break;

We would like to share two examples to display the working functionality of the JavaScript Break statement in both For loop and While loop

JavaScript Break Statement inside For Loop

In this program, We are going to use the break statement inside the JavaScript For Loop to exit from the loop iteration.

<!DOCTYPE html>
<html>
<head>
    <title> JavaScript Break Statement in For Loop </title>
</head>

<body>
    <h1> JavaScript Break Statement </h1>
<script>
    var i;
    for (i = 10; i > 0; i--)
    {
        if(i == 6)
        {
            document.write("<br \>Coming Out of <b> For Loop</b> Where i = " + i);
            break;
        }
        
        document.write("<br \ ><b>Numbers </b>= " + i); 
    } 
</script>
</body>
</html>
JavaScript Break Statement 1

Within the For loop we initialized the value of i as: i =10. And, we used Decrement operator to decrement the value by 1. If you find any difficult to understand the For loop, please visit our article: JavaScript For Loop

Inside the JavaScript For loop we placed JavaScript If statement to test whether I is equal to 6. If the condition is false then it will skip the Break statement and prints that number as output (In Our case 10, 9, 8, 7).

If this condition is True, then JS Break statement will be executed. Next, the iteration will stop at that number without printing the other statements

JavaScript Break Statement inside While Loop

We are going to use the break statement inside the While loop to exit from the loop iteration.

<!DOCTYPE html>
<html>
<head>
    <title> JavaScript Break Statement in For Loop </title>
</head>

<body>
    <h1> JavaScript Break Statement </h1>
<script>
    var i = 0;
    while (i <= 10)
    {
        document.write("<br \ ><b>The Value of the Variable </b>= " + i);
        i++;
        
        if(i == 4)
        {
            break;
        }
    }
    document.write("<br \>This statement is from Outside <b> While Loop</b> = " + i);
</script>
</body>
</html>
JavaScript Break Statement

The Value of the Variable = 0
The Value of the Variable = 1
The Value of the Variable = 2
The Value of the Variable = 3
This statement is from Outside While Loop = 4

We initialized the value of i as: i = 0 at the beginning of the code. Within the While loop, we check for the condition whether i is less than or equal to 10 or not. If you find any difficult to understand the While loop, please visit our article: JavaScript While Loop

Inside the While loop, we placed if statement to test whether i equal to 4. If the condition is false, then it will skip the Break statement and prints that number as output (In Our case 0, 1, 2, 3).

If this condition is True, the JS Break statement will execute. Next, the iteration will stop at that number without printing the other printf statement.