# JavaScript Switch Statement
● The JavaScript Switch Statement
Use the switch statement to select one of many blocks of code to be executed.
● The break keyword
When JavaScript reaches a break keyword, it breaks out of the switch block.
this will stop the execution of more code and case testing inside the block.
When a match is found, and the job is done, it's time for a break. There is no need for more testing. It is not necessary to break the last case in a switch block. The block breaks there anyway.
this will stop the execution of more code and case testing inside the block.
When a match is found, and the job is done, it's time for a break. There is no need for more testing. It is not necessary to break the last case in a switch block. The block breaks there anyway.
● The default keyword
The default keyword specifies the code to run if there is no case match.
The default case does not have to be the last case in a switch block.
The default case does not have to be the last case in a switch block.
● Common Code Blocks
Sometimes programmer will want different switch cases to use the same code.
In this example case 4 and 5 share the same code block, and 0 and 6 share another code block.
In this example case 4 and 5 share the same code block, and 0 and 6 share another code block.
No comments:
Post a Comment