JavaScript switch statement
- Previous Page Return
- Next Page Throw
- Go Up One Level JavaScript Statement Reference Manual
Definition and Usage
The switch statement executes code blocks based on different situations.
The switch statement is part of JavaScript's 'conditional' statements, used to execute different operations based on different conditions. Use switch to select one of many code blocks to be executed. This is the perfect solution for long nested if/else statements.
The switch statement calculates the expression. Then, the value of the expression is compared with the value of each case in the structure. If a match is found, the associated code block is executed.
The switch statement is usually used with the break or default keyword (or both). These are optional:
The break keyword exits the switch block. This will stop executing more code and/or case tests within the block. If break is omitted, the next code block in the switch statement is executed.
If no case matches, the default keyword specifies some code to be executed. Only one default keyword can be used in a switch. Although it is optional, it is recommended to use it because it can handle unexpected situations.
Example
Execute code blocks based on user input:
var text; var fruits = document.getElementById("myInput").value; switch(fruits) { case "Banana": text = "Banana is good!"; break; case "Orange": text = "I am not a fan of orange."; break; case "Apple": text = "How you like them apples?"; break; default: text = "I have never heard of that fruit..."; }
More TIY examples are available at the bottom of the page.
Syntax
switch(expression) { case n: code block break; case n: code block break; default: default code block }
Parameter value
Parameter | Description |
---|---|
expression | Required. Specify the expression to be calculated. The expression is calculated once. The value of the expression is compared with the value of each case label in the structure. If a match is found, the corresponding code block is executed. |
Technical details
JavaScript version: | ECMAScript 1 |
---|
More examples
Example
Calculate the weekday name using the weekday number of today (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; default: day = "Unknown Day"; }
Example
If today is neither Saturday nor Sunday, write 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"; }
Example
Sometimes you may want different conditions to use the same code, or use the same default value.
Please note that in this example, cases share the same code block, and the default case does not have to be the last case in the switch block (but if the default is not the last case in the switch block, remember to end it with a break).
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
Execute code blocks using the switch statement based on user input from the prompt box:
var text; var favDrink = prompt("What's 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 the Cosmopolitan is your favorite?"; break; default: text = "I have never heard of that one.."; }
Browser Support
Statement | Chrome | IE | Firefox | Safari | Opera |
---|---|---|---|---|---|
switch | Supported | Supported | Supported | Supported | Supported |
Related Page
JavaScript Tutorial:JavaScript If...Else Statement
JavaScript Tutorial:JavaScript Switch Statement
JavaScript Reference Manual:JavaScript If/Else Statement
JavaScript Reference Manual:JavaScript Break Statement
- Previous Page Return
- Next Page Throw
- Go Up One Level JavaScript Statement Reference Manual