The JavaScript switch statement is used to execute one code from multiple expressions.
A typical switch statement is
- the first
switch
, followed by an expression which is often referred to as the control expression or control variable of the switch statement - subsequent lines defining the actual cases (the values), with corresponding sequences of statements for execution when a match occurs
- a
break
statement typically follows acase
statement to end said statement.
Each alternative begins with the particular value, or list of values , that the control variable may match and which will cause the control to goto the corresponding sequence of statements. The value (or list/range of values) is usually separated from the corresponding statement sequence by a colon. Every case must also be preceded by a keyword such as case
.
An optional default case is typically also allowed, specified by a default
keyword. This executes when none of the other cases match the control expression.
syntax
switch(expression to be tested) { case value1: code to be executed if a match; break; case value2: code to be executed if a match; break; default: code to be executed if above values are not matched; }
Examples
This example has a preset value
<html> <body> <script> var grade='A'; var result; switch(grade) { case 'A': result="A Grade"; break; case 'B': result="B Grade"; break; case 'C': result="C Grade"; break; default: result="No Grade"; } document.write(result); </script> </body> </html>
This example displays the day by using the getDay()
method which returns the weekday as a number between 0 and 6.
<!DOCTYPE html> <html> <body> <script> switch (new Date().getDay()) { case 0: day = "Sunday"; break; case 1: day = "Monday"; break; case 2: day = "Tuesday"; break; case 3: day = "Wednesday"; break; case 4: day = "Thursday"; break; case 5: day = "Friday"; break; case 6: day = "Saturday"; } document.write(day); </script> </body> </html>
The break Keyword
When JavaScript reaches a break
keyword, it breaks out of the switch block. This will stop the execution inside the switch block.
It is not necessary to break the last case in a switch block.
If you omit the break statement, the next case will be executed even if the evaluation does not match the case. This can be a common programming error. Modify the above code example and run this and see what happens, especially on Sunday
<!DOCTYPE html> <html> <body> switch (new Date().getDay()) { case 0: day = "Sunday"; case 1: day = "Monday"; case 2: day = "Tuesday"; case 3: day = "Wednesday"; case 4: day = "Thursday"; case 5: day = "Friday"; case 6: day = "Saturday"; } document.write(day); </script> </body> </html>
The default Keyword
The default
keyword specifies the code to run if there is no case match:
In this example if its not Saturday or Sunday the default will be printed which is Work day – sorry
<!DOCTYPE html> <html> <body> <script> switch (new Date().getDay()) { case 6: day = "It is Saturday"; break; case 0: day = "It is Sunday"; break; default: day = "Work day - sorry"; } document.write(day); </script> </body> </html>