JavaScript switch
Statement
JavaScript Statements Reference Manual
Example
Display the name of the day of the week (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;
}
day output result is:
var d = new Date().getDay();
switch (d) {
case 0:
x = "Sunday";
break;
case 1:
x = "Monday";
break;
case 2:
x = "Tuesday";
break;
case 3:
x = "Wednesday";
break;
case 4:
x = "Thursday";
break;
case 5:
x = "Friday";
break;
case 6:
x = "Saturday";
break;
}
document.write(x);
More examples are included at the bottom of this article.
Definition and Usage
The switch
statement is used to perform different actions based on different conditions.
The switch
statement is part of JavaScript conditional statements, used to execute different actions based on different conditions. It selects one block of code to be executed from multiple blocks.
The switch
statement evaluates each expression. The value of the expression is then compared with the values of each case in the structure. If a match is found, the associated block of code is executed.
The switch
statement is often used with the break
or default
keywords, both of which are optional:
break keyword is used to break out of the switch block. It terminates the execution of the switch block. If this keyword is omitted, the next block of code in the switch statement is executed.
default keyword specifies what to do when no match is found.
The default
keyword can only appear once in a switch statement. Although it is optional, it is recommended to use it to output information when the result is not as expected.
Browser Support
Statement | Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|---|
switch | Yes | Yes | Yes | Yes | Yes |
Syntax
Parameter Values
Parameter | Description |
---|---|
expression | Required. Specifies the expression to be evaluated. The expression is evaluated only once. The value of the expression is compared with the values of each case. If a match is found, the associated block of code is executed. |
Technical Details
| JavaScript Version: | 1.2 |
More Examples
Example
If today is neither Saturday nor Sunday, output a 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";
}
text output result is:
switch (new Date().getDay()) {
case 6:
x = "Today is Saturday";
break;
case 0:
x = "Today is Sunday";
break;
default:
x = "Looking forward to the Weekend";
}
document.write(x);
Example
Sometimes you may want to use the same code for multiple cases or set a common default value.
Note that in this example, 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";
}
Example
Using the switch statement to evaluate user input:
var text;
var favDrink = prompt("What's your favorite cocktail drink?");
switch (favDrink) {
case "Martini":
text = "Great choice! Martini is good for your soul.";
break;
case "Daiquiri":
text = "Daiquiri is also my favorite!";
break;
case "Cosmopolitan":
text = "Really? Are you sure Cosmopolitan is your favorite?";
break;
default:
text = "I don't have a favorite...";
break;
}
Related Pages
JavaScript Tutorial: JavaScript If...Else Statement
JavaScript Tutorial: JavaScript Switch Statement
JavaScript Reference Manual: JavaScript if/else Statement