We’ve learned that a switch case statement is a conditional statement that compares a variable to multiple values and executes different code blocks based on the value of the variable. In this guide, we’ve covered the basics of the switch case statement in JavaScript. If score equals 80, for example, the code block following the second case will be executed and the message “B” will be displayed to the user. We then use a switch case statement to compare score to multiple values (90, 80, 70, 60, etc.) and display a message to the user based on the value of score. In this example, we declare a variable score and assign it the value 80. In the following example, we use the switch case statement to determine the grade of a student based on their score:Ĭase ( score >= 90) : console. You can also use the switch case statement with multiple conditions. Example: Switch Case Statement with Multiple Conditions If day equals “Monday”, for example, the code block following the first case will be executed and the message “Today is Monday.” will be displayed to the user. We then use a switch case statement to compare day to multiple values (“Monday”, “Tuesday”, “Wednesday”, etc.) and display a message to the user based on the value of day. In this example, we declare a variable day and assign it the value “Monday”. log( "Today is Friday.") ĭefault : console. log( "Today is Thursday.") Ĭase "Friday" : console. log( "Today is Wednesday.") Ĭase "Thursday" : console. log( "Today is Tuesday.") Ĭase "Wednesday" : console. log( "Today is Monday.") Ĭase "Tuesday" : console.
0 Comments
Leave a Reply. |