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

Syntax

In 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

expression

An expression matched against each case clause.

case valueN

A case clause used to match against expression.

statementsN

Statements that are executed if the expression matches the associated case clause.

Example

In 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 Now


Browser 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