Jsref Switch
[ JavaScript Statement Reference Manual](#)
## Examples
Display the name of today's day (Sunday=0, Monday=1, Tuesday=2, ...):
var day; 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"; break; }
The output of _day_ is:
Saturday
[Try it Β»](#)
More examples are included at the bottom of this page.
* * *
## Definition and Usage
The switch statement is used to perform different actions based on different conditions.
The switch statement is part of JavaScript's conditional statements, used to execute different actions based on different conditions. Use switch to select one of many code blocks to be executed.
The switch statement evaluates each expression. The value of the expression is then compared to the values of each case in the structure. If a match is found, the code block associated with that case is executed.
The switch statement is often used together with the break or default keywords. Both are optional:
The **break** keyword is used to exit the switch code block. It terminates the execution of the switch code block. If this keyword is omitted, the next code block in the switch statement will be executed.
The **default** keyword specifies what to do when there is no match. The default keyword can only appear once in a switch statement. Although it is an optional parameter, it is recommended to use it to handle cases that are not expected.
* * *
## Browser Support
| Statement | | | | | |
| --- | --- | --- | --- | --- | --- |
| switch | Yes | Yes | Yes | Yes | Yes |
* * *
## Syntax
switch(_expression_) {
case _n_:
_code block_ break;
case _n_:
_code block_ break;
default:
_default code block_}
## Parameter Values
| Parameter | Description |
| --- | --- |
| _expression_ | Required. Specifies the expression to evaluate. The expression is evaluated only once. The value of the expression is compared to the values of each case in the structure. If a match is found, the code block associated with that case is executed. |
## Technical Details
| JavaScript Version: | 1.2 |
| --- |
* * *

## More Examples
## Example
If today is not Saturday or Sunday, output the default message:
var text; switch(new Date().getDay()){case 6: text = "Today is Saturday"; break; case 0: text = "Today is Sunday"; break; default: text = "Looking forward to the Weekend"; }
The output of _text_ is:
Today is Saturday
[Try it Β»](#)
## Example
Sometimes you want to use the same code for different cases. Or set a common default value.
Note that in case 3, the cases share common code, and the default statement is not at the end of the switch statement:
var text; switch(new Date().getDay()){case 1: case 2: case 3: default: text = "Looking forward to the Weekend"; break; case 4: case 5: text = "Soon it is Weekend"; break; case 0: case 6: text = "It is Weekend"; }
[Try it Β»](#)
## Example
Use the switch statement to evaluate user input:
var text; var favDrink = prompt("What is your favorite cocktail drink?"); switch(favDrink){case"Martini": text = "Best choice! Martini is good for your soul. "; break; case"Daiquiri": text = "Daiquiri is also a favorite of mine!"; break; case"Cosmopolitan": text = "Really? Are you sure Cosmopolitan is your favorite?"; break; default: text = "I don't have a favorite..."; break; }
[Try it Β»](#)
* * *
## Related Pages
JavaScript Tutorial: [JavaScript If...Else Statement](#)
JavaScript Tutorial: (#)
JavaScript Reference: [JavaScript if/else Statement](#)
* * JavaScript Statement Reference Manual](#)
YouTip