
Switch Statement in JavaScript



Switch Statement in JavaScript
The switch statement is used to perform different action based on multiple cases. How does switch statement work?- Firstly, evaluate the expression once.
- Value of expression will be compared with each case.
- If there would be a match, the associated block of code will execute.
Syntax switch (expression) { case x: // case x code block (statement x) break; case y: // case y code block (statement y) break; default: // execute default code block (statement default) }
Break Keyword in JavaScript Switch Statement
As the name says break, When JavaScript reaches the break keyword, it breaks out of the switch block. It will only stop the execution of inside the block. It will continue until reaching the last break case. Eventually, reach the last case and the break keyword will end the switch block.Default Keyword in JavaScript Switch Statement
If there is no case match then the default code block will run. There could be only one default keyword in the switch statement. The default keyword is optional, but it is recommended that you use it. Let’s see an example of a switch statement.



Multiple Cases in JavaScript Switch Statement
There is a chance that you may face a code in which you have to display the same output for multiple cases. So, we have made an example for you to understand these multiple cases.

×
![]()