In JavaScript, the switch statement is used to evaluate an expression and execute different code blocks based on different cases.
Syntax
switch (expression) {
case value1:
// code block
break;
case value2:
// code block
break;
default:
// code block
}
The expression
is evaluated once, and the value of the expression is compared with each case
value. If there is a match, the code block associated with that case is executed. If there is no match, the code block associated with the default
case is executed (if there is one).
The break
statement is used to exit the switch statement and prevent the code from executing the code in the other cases.
Example
let day = 2;
let dayName;
switch (day) {
case 1:
dayName = "Sunday";
break;
case 2:
dayName = "Monday";
break;
case 3:
dayName = "Tuesday";
break;
case 4:
dayName = "Wednesday";
break;
case 5:
dayName = "Thursday";
break;
case 6:
dayName = "Friday";
break;
case 7:
dayName = "Saturday";
break;
default:
dayName = "Invalid day";
}
console.log(dayName); // "Monday"
In this example, the day
variable is assigned the value of 2
. The switch statement then evaluates the day
variable and compares it to each case value. Since day
equals 2
, the code block associated with the case 2:
label is executed, which assigns the dayName
variable the value of "Monday"
. The break
statement is then used to exit the switch statement.
Multiple Cases
It is possible to have multiple cases share the same code block in a switch statement. To do this, separate the case values with commas:
let fruit = "orange";
let color;
switch (fruit) {
case "apple":
case "cherry":
case "strawberry":
color = "red";
break;
case "banana":
case "lemon":
color = "yellow";
break;
case "orange":
color = "orange";
break;
default:
color = "unknown";
}
console.log(color); // "orange"
In this example, the code block for the case "apple":
, case "cherry":
, and case "strawberry":
labels is the same.
FAQs
- What is a switch statement in JavaScript? A switch statement is a control statement in JavaScript that evaluates an expression and executes different code blocks based on different cases.
- How does a switch statement work in JavaScript? The switch statement evaluates an expression and compares the value of the expression to each case value. If there is a match, the code block associated with that case is executed. If there is no match, the code block associated with the
default
case is executed (if there is one). - What is the syntax of a switch statement in JavaScript? The basic syntax of a switch statement in JavaScript is
switch (expression) { case value1: // code block break; case value2: // code block break; default: // code block }
. - Is it possible to use a switch statement with strings? Yes, you can use a switch statement with strings in JavaScript. Starting from ECMAScript 6, you can use template literals to specify the cases as strings, like this
let fruit = 'apple';
switch (fruit) {
case `apple`:
console.log(`I like apples`);
break;
case `banana`:
console.log(`I like bananas`);
break;
default:
console.log(`I don't like ${fruit}`);
break;
}