Javascript TUTORIAL
Switch statement sharing same code
If you have a the switch statement where several switch 'cases' share same properties, then we can combine them in such a way that only that switch statement need to have the code.
Let us take a look at the example where we find if the height of a person given in inches is less than, equal to or greater than 5 feet.
|
You can try this example online here.
Javascript switch Combime similar statements
- If the height is 58 or 59, the case statement next to 59 is executed.
- If the height is 61,62 or 63, the case statement next to 63 is executed.
Rest of the code is self explanatory.
You may be tempted to think that you can combine two case options using and OR operator as in
|
or, using a list of options in this manner
|
Both of these are syntactically incorrect.