JavaScript switchStatement

JavaScript 语句参考手册JavaScript Statement Reference

Example

Display today's weekday name (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; }

dayOutput:


Try it Yourself »

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's conditional statements, used to perform different actions based on different conditions. Use switch to select one of many blocks of statements to be executed.

The switch statement evaluates an expression. The value of the expression is then compared with the values of each case in the structure. If there is a match, the code block associated with that case is executed.

The switch statement is often used together with the break or default keyword. Both are optional:

breakThe keyword is used to break out of 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.

defaultThe keyword specifies what to do when there is no match. The default keyword can only appear once in a switch statement. Although it is optional, it is recommended to use it; in cases that are not what we expect, you can use it to output information.


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 be evaluated. The expression is evaluated only once. The value of the expression is then compared with the values of each case in the structure. If there is a match, the code block associated with that case is executed.

Technical Details

JavaScript Version: 1.2


实例s

More Examples

Example

If today is not Saturday and not 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"; }

textOutput:


Try it Yourself »

Example

Sometimes you want different cases to use the same code. Or set a common default value.

Note that in the 3 cases, the cases use common code, and the default statement is not at the very 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 Yourself »

Example

Use a switch statement to determine the content entered by the user:

var text; var favDrink = prompt("What is your favorite cocktail drink?"); switch(favDrink) { case "Martini": text = "Excellent choice! Martini is good for your soul."; break; case "Daiquiri": text = "Daiquiri is my favorite too!"; break; case "Cosmopolitan": text = "Really? Are you sure Cosmopolitan is your favorite?"; break; default: text = "I don't have a favorite..."; break; }

Try it Yourself »


Related Pages

JavaScript Tutorial:JavaScript If...Else Statement

JavaScript Tutorial:JavaScript Switch Statement

JavaScript Reference:JavaScript if/else Statement


JavaScript 语句参考手册JavaScript Statement Reference Other Extensions