Break Statement in JavaScript | Example Program

A break statement in JavaScript that breaks out a loop or switch statement. It simply tells the browser to exit the code block and move on the next line of code (if any) after the block.

When a break statement executes inside a loop statement, the loop immediately ends at a specified condition.

As a result, the control continues the execution of the next statement immediately following the loop body.

Similarly, when break statement encounters inside the switch block, it terminates ‘case’ inside the switch block and stops the execution of more cases inside the switch.

When the loops are nested, the break statement breaks only the inner loop. We can use JavaScript break statement in all types of loops, such as for loop, while loop, and do-while loop.

Syntax of Break Statement


The syntax to use break statement in JavaScript is as follows:

// Jump statement
   break;

The flowchart of break statement in JavaScript has shown in the below figure.

Flowchart diagram of break statement in JavaScript
You can also use the break statement with a label reference, to break out of any JavaScript code block. The general syntax to use break statement with label is as:

break labelName;

Without a label, you can only use break inside a loop or a switch.

Use of Break Statement


Break statement in JavaScript can be used in three ways that are:

1. We can use a break inside the body of loop to come out of it. Look at the below figure to understand better.

Example of break statement

2. We can use it inside the switch block to come out of switch block.


3. We can also use it inside the nested blocks to go to the end of the block.

Example Program based on Break


1. Let’s take a simple example program in which we will use a break statement inside the for loop for breaking the loop.

Program code 1:

<script>
// Using for loop.
  for(let i = 1; i <= 10; i++)
  {
    if(i == 5) break; // Breaking a loop.
    document.write("I = " +i, "<br>");
  }
</script>
Output:
     I = 1
     I = 2
     I = 3
     I = 4

2. Let’s create another program in which we will use break statement inside the inner for loop to exit from loop.

Program code 2:

<script>
// Outer for loop.
  for(let i = 1; i <= 3; i++)
  {
// Inner for loop.
   for(let j = 0; j <= 3; j++)
   {
    if(i == 2 && j == 2)
    break; // Using break statement inside for loop.
    document.write(i+" "+j, "<br>");
   }
 }
</script>
Output:
     1 0
     1 1
     1 2
     1 3
     2 0
     2 1
     3 0
     3 1
     3 2
     3 3

3. Let’s create a program where we will use break statement with labeled for loop. We will use break with the label to exit from the loop.


Program code 3:

<script>
// Outer for loop.
  for(let i = 1; i <= 3; i++)
  {
   bb:
// Inner for loop.
   for(let j = 1; j <= 3; j++)
   {
    if(i == 2 && j == 2)
      break bb; // Using break statement with label.
    document.write(i+" "+j, "<br>");
   }
 }
</script>
Output:
     1 1
     1 2
     1 3
     2 1
     3 1
     3 2
     3 3

4. Let’s create a program where we will use break statement inside while loop to exit from loop.

Program code 4:

<script>
// while loop.
   let i = 1;
   while(i <= 10) {
   if(i == 5) {
     i++;
    break; // Breaking the loop.
   }
  document.write(i, "<br>");
  i++;
 }
</script>
Output:
    1
    2
    3
    4

5. Let’s create a program where we will use break statement inside the do-while loop to exit from the loop.

Program code 5:

<script>
// Initialization.
   let i = 1;
// do-while loop.
   do {
   if(i == 5) {
     i++;
     break;
    }
   document.write(i, "<br>");
   i++;
  } while(i <= 10);
</script>
Output:
     1
     2
     3
     4

More Example based on Break Statement

Program code 6:

<script>
   const stNames = ["Deepak", "Ivaan", "Hileri", "John"];
   let text = "";
   list: {
      text += stNames[0] + "<br>";
      text += stNames[1] + "<br>";
      text += stNames[2] + "<br>";
      break list;
      text += stNames[3] + "<br>";
    }
   document.write(text);
</script>
Output:
     Deepak
     Ivaan
     Hileri

In this tutorial, you learned how to use the break statement in JavaScript to exit from the loop. Hope that you will have understood the basic concepts of break statement.

In the next tutorial, we will discuss continue statement in JavaScript with example programs.
Thanks for reading!!!
Next ⇒ Continue Statement in JavaScript⇐ PrevNext ⇒

Please share your love