How to use the JavaScript Switch statement
This article mainly introduces "how to use JavaScript Switch sentences". In daily operation, I believe many people have doubts about how to use JavaScript Switch sentences. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use JavaScript Switch sentences". Next, please follow the editor to study!
Use the switch statement to select one of multiple blocks of code that need to be executed.
Grammar
Switch (expression) {
Case n:
Code block
Break
Case n:
Code block
Break
Default:
Default code block
}
Code interpretation:
Evaluate the switch expression once
Compare the value of the expression with the value of each case
If there is a match, the associated code is executed
Example
The getDay () method returns a week name number (weekday number) between 0 and 6.
(Sunday=0, Monday=1, Tuesday=2)
This example uses the week name number to calculate the week's name:
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:
Today is Thursday.
At this point, the study of "how to use JavaScript Switch sentences" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!