How to come out of a switch case in JavaScript?


In this tutorial, we will learn to come out of a switch case in JavaScript. The switch-case statement is the advanced version of the too many complicated if-else statements. Suppose that in the code, you apply too many conditions to the if-else statement; it will become complicated.

Users can see the example of too many if-else here.

if( condition 1) {
   // some code
} else if( condition 2 ) {
   // some code for this block
} else if( condition 3 ) {
   // some code for this block
} else {
   // some code
}

In the above cases, it is better to use the switch-case statement, which makes syntax easy and readable. Also, it required to learn to break the switch-case statement. Otherwise, it will execute different cases continuously.

Use the break Keyword to Come Out of a Switch Case

In JavaScript, the break keyword is used to stop the execution of the switch case and comes out of it. We can apply the break keyword after every case of the switch case if we want to execute only a single case from the switch case. If we don’t apply the break keyword, the switch case continues to execute all cases and comes out when all cases will be finished.

Syntax

Users can follow the syntax below to use the break keyword with the switch case.

switch (expression) {
   case "A":
 
      // code for case "A". 
   break;
   case "B":
 
      // code for case "B".
   break;
   default:
   // code for default case break;
}

Keywords

  • expression − It is the expression for the switch case, which will be matched with every case, and expression matches with any case; it will execute that block of code.

  • case − It is used to define the various cases for the switch-case statement.

  • default − If the expression does not match any case, it will execute the default case, defined with the “default” keyword

  • break − It is used to come out from any case

Example

In the example below, we have created the switch case with various cases and applied the break keyword after some cases come out from the statement. Also, we have defined the default case. Furthermore, we are executing the switch case for the different cases, and users can observe the result in the output.

<html> <head> </head> <body> <h2>Comes out of switch case in JavaScript.</h2> <h4>Using the <i>break</i> keyword to stop the execution of the switch case.</h4> <p id="output"></p> <script> let output = document.getElementById("output"); function matchCase(expression) { switch (expression) { case "A": output.innerHTML += "Control is in A case. <br/> "; break; case "B": output.innerHTML += "Control is in B case. <br/> "; break; case "C": output.innerHTML += "Control is in C case. <br/> "; case "D": output.innerHTML += "Control is in D case. <br/> "; break; default: output.innerHTML += "Control is in Default case. <br/> "; break; } } // calling the function for different expressions output.innerHTML += "<br/>"; matchCase("A"); output.innerHTML += "<br/>"; matchCase("Z"); output.innerHTML += "<br/>"; matchCase("C") </script> </body> </html>

In the above output, users can see that executing the switch case for expression “A” stops execution after the block terminates. Control goes to the default case for the expression “Z,” as we haven’t defined the “Z” case in the switch statement. For the expression “C,” control goes to the case “D” also, as we haven’t added the break keyword after the statement of case “C.”

In this tutorial, we have learned to break the switch case statement. The thing is that users need to add the break keyword at the end of the block of code of the switch-case statement if they want to terminate the execution from that case.

Updated on: 23-Aug-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements