JS Switch
The switch statement is used to perform different actions based on different conditions.
The switch statement is used to perform different actions based on different conditions.
The JavaScript Switch Statement
Use the switch statement to select one of many code blocks to be executed.
This is how it works:
- The switch expression is evaluated once.
- The value of the expression is compared with the values of each case.
- If there is a match, the associated block of code is executed.
- If there is no match, the default code block is executed.
Preview
The break Keyword
When JavaScript reaches a break keyword, it breaks out of the switch block.
This will stop the execution of inside the switch block.
It is not necessary to break the last case in a switch block. The block breaks (ends) there anyway.
The default Keyword
The default keyword specifies the code to run if there is no case match.
Preview