JavaScript Switch Case
JavaScript Switch Case Statement
The switch statement is used to perform different actions based on different conditions
If you have multiple case statements, and you want to evaluate all cases with different result you might consider using the javascript switch case statement
SyntaxIn the above javascript switch case syntax, the expression would be the key condition we want evaluate. Lets the expression as a number. In the case 1 if the number is odd it will execute some statement, in the case two, if the number is even then executed another statement and so on...
Values
expressionAn expression matched against each case clause.
case valueNA case clause used to match against expression.
statementsNStatements that are executed if the expression matches the associated case clause.
ExampleIn the above example, if color evaluates to "red", the program matches the value with case "red" and executes the associated statement. When a break is encountered, the program breaks out of the switch and executes the statement following switch. If break were omitted, the statement for case "My Car is purple" would also be executed.
Try It NowBrowser compatibility
Feature | |||||||
---|---|---|---|---|---|---|---|
Basic support | Yes | Yes | Yes | Yes | Yes | Yes | Yes |
Practice with out Interactive Editor and take your JavaScript Skills to the next level
Exercise 1 Exercise 2 Exercise 3 Exercise 4 Exercise 5 Exercise 6
Complete Beginner to JavaScript Developer - Udemy
Advanced JavaScript Development - Udemy
Learn javascript in one month from experts -OneMonth
Become a Certified Web Developer - Udemy