English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
JavaScript Statements and Variable Declarations
switchStatements are a feature ofConditional statementOne of them, used to perform different actions under different conditions.
switchEvaluate the expression, match the expression value with the case clause, and execute the statements associated with the case, as well as the statements after the matching case.
Associated with each case labelbreakEnsure that once a matching statement is executed, the program will exit the switch and continue executing the statements after switch. If break is omitted, the program will continue executing the next statement after the switch statement.
defaultSpecify some code to be executed by default if no match is found. There can only be one default statement in switch. Although it is optional, it is recommended to use it because it can handle unexpected situations.
Use switch to select one of many code blocks to be executed. This is a perfect solution for long nested if ... else statements.
switch (expression) { case value1: //When the statement is executed //Result of expression match value1 break; case value2: //When the statement is executed //Result of expression match value2 break; ... case valueN: //When the statement is executed //Result of expression match valueN break; default: //Statement executed when none of the above conditions are met //These values match the value of the expression }
var city = document.querySelector("input").value; var text; switch (city) { case "Jaipur": text = "Jaipur is known as the Pink City"; break; case "Bengaluru": text = "Bengaluru is known as the IT city"; break; case "Kerala": text = "Kerala God39;s Own Country"; break; default: text = "I have never heard of that city..."; }Test and See‹/›
All browsers fully support the switch statement:
Statement | |||||
switch | Is | Is | Is | Is | Is |
Parameter | Description |
---|---|
expression | An expression whose result matches each case clause. |
case valueN | Case clauses used to match expressions. If the expression matches the specified valueN, the statements in the case clause are executed until the end of the switch statement or 'break' is encountered. |
default | (Optional) Default clause; if provided, this clause is executed if the value of expression does not match any case clause. |
JavaScript version: | ECMAScript 1 |
---|
Use the working day number to calculate the name of the working day (Sunday = 0, Monday = 1,) 1And so on...):
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 = "Undefined Day"; }Test and See‹/›
If you forget to use 'break', the script will start running from the conditions that match and then run the following conditions under the conditions that match:
var num = Number(document.querySelector("input").value); var text; switch (num) { case 1The number you entered is 1"; case 2The number you entered is 2"; case 3The number you entered is 3"; case 4The number you entered is 4"; case 5The number you entered is 5"; default: text = "Default statement executed"; }Test and See‹/›
Sometimes, you may want the same code to be used for different cases, or use general code.
This is an example of a single-action switch statement where the same operation is executed for four different values:
var num = Number(document.querySelector("input").value); var text; switch (num) { case 1: case 2: case 3: case 4: text = "The number you entered is between" 1 - 4"; break; case 5: case 6: case 7: case 8: text = "The number you entered is between" 5 - 8"; break; default: text = "Default statement executed"; }Test and See‹/›
JavaScript Tutorial:JavaScript If ... Else Statement
JavaScript Tutorial:JavaScript switch
JavaScript Reference:JavaScript break Statement