JavaScript Switch Statements
- Previous Page JS Condition
- Next Page JS Loop For
switch
statement is used to execute different actions based on different conditions.
JavaScript Switch Statements
Please use switch
A statement is used to select one of the multiple code blocks to be executed.
Grammar
switch(表达式) { case n: Code block break; case n: Code block break; default: Default code block }
Code Explanation:
- Calculate the switch expression once
- Compare the value of the expression with the value of each case
- If there is a match, execute the associated code
Example
The getDay() method returns the weekday number (0 to 6) between 0 and 6.
(Sunday=0, Monday=1, Tuesday=2 ..)
In this example, the name of the week is calculated using the weekday number:
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"; }
The result will be:
break keyword
If JavaScript encounters break
keyword, which will jump out of the switch code block.
This will stop the execution of more code in the code block and case testing.
If a match is found and the task is completed, randomly interrupt the execution (break). No further testing is needed.
break
It can save a lot of execution time because it will "ignore" the execution of other code in the switch code block.
There is no need to break the last case in the switch code block. The code block will naturally end here.
default keyword
default
The keyword specifies the code to be executed when no case matches:
Example
getDay()
The method returns the number of the weekday (0 to 6).
If today is neither Saturday (6) nor Sunday (0), output a default message:
switch (new Date().getDay()) { case 6: text = "It's Saturday today"; break; case 0: text = "It's Sunday today"; break; default: text = "Looking forward to the weekend~"; }
The result of text is:
Defaultthe case does not have to be the last case in the switch code block:
Example
switch (new Date().getDay()) { default: text = "Looking forward to the weekend!"; break; case 6: text = "It's Saturday today"; break; case 0: text = "It's Sunday today"; }
If default
If not the last case in the switch code block, remember to use break to end the default case.
Common code blocks
Sometimes you may need different cases to use the same code.
In this example, case 4 and 5 share the same code block, while 0 and 6 share another code block:
Example
switch (new Date().getDay()) { case 4: case 5: text = "The weekend is approaching:("; break; case 0: case 6: text = "It's the weekend~"; break; default: text = "Looking forward to the weekend!"; }
Details of Switching
If multiple cases match a case value, the first case is selected.
If the matching case is not found, the program will continue to use the default label.
If the default label is not found, the program will continue with the statement after the switch.
Strict Comparison
Switch case uses strict comparison (===
).
The value must be the same as the type to be matched.
Strict comparison can only be true when the operands belong to the same type.
In this example, x will not match:
Example
var x = "0"; switch (x) { case 0: text = "Off"; break; case 1: text = "On"; break; default: text = "No value found"; }
Supplementary Books
For more information on JavaScript Switch StatementsFor more information on the knowledge, please read the relevant content in the Advanced JavaScript Tutorial:
- ECMAScript switch Statement
- The switch statement is a sibling statement of the if statement. This section introduces the usage of the switch statement and the differences from the switch statement in Java.
- Previous Page JS Condition
- Next Page JS Loop For