Grammar
Switch (n) {Case 1: Execute code block 1 break;case 2: Execute code block 2 break;default: N code that executes when different from 1 and 2
How it works: first set an expression n (usually a variable). The value of the subsequent expression is compared to the value of each case in the structure. If there is a match, the code block associated with the case is executed. Use break to prevent your code from automatically running down a case.
Instance
Displays the week name for today. Please note that sunday=0, Monday=1, tuesday=2, etc.:
var day=new Date (). GetDay (); switch (day) {case 0: x= "Today it ' s Sunday"; Break;case 1: x= "Today it ' s Monday"; Break;case 2: x= "Today it ' s Tuesday"; Break;case 3: x= "Today it ' s Wednesday"; Break;case 4: x= "Today it ' s Thursday"; Break;case 5: x= "Today it ' s Friday"; Break;case 6: x= "Today it ' s Saturday"; Break
}
Use switch in JS