Explain Javascript Switch Statement
The switch
statement in JavaScript is used to execute one block of code among many options. It is an alternative to using multiple if...else if
statements, making the code more readable and easier to manage when dealing with multiple possible values for a single variable.
Here's the basic syntax of a switch
statement:
switch (expression) {
case value1:
// Code to be executed if expression === value1
break;
case value2:
// Code to be executed if expression === value2
break;
// You can have as many cases as needed
default:
// Code to be executed if expression doesn't match any case
}
Example 1: Basic Switch Statement
Let's use a switch statement to display the name of the day based on a number (0 for Sunday, 1 for Monday, etc.).
let day = 3;
let dayName;
switch (day) {
case 0:
dayName = "Sunday";
break;
case 1:
dayName = "Monday";
break;
case 2:
dayName = "Tuesday";
break;
case 3:
dayName = "Wednesday";
break;
case 4:
dayName = "Thursday";
break;
case 5:
dayName = "Friday";
break;
case 6:
dayName = "Saturday";
break;
default:
dayName = "Invalid day";
}
console.log(dayName); // Output: Wednesday
Example 2: Using Default Case
If the value of the expression doesn't match any of the specified cases, the code inside the default
case will be executed.
let fruit = "Mango";
let color;
switch (fruit) {
case "Banana":
color = "Yellow";
break;
case "Apple":
color = "Red";
break;
case "Grapes":
color = "Purple";
break;
default:
color = "Unknown";
}
console.log(color); // Output: Unknown
Example 3: Grouping Cases
You can group cases to execute the same code for multiple values.
let animal = "Dog";
let sound;
switch (animal) {
case "Cow":
case "Goat":
case "Sheep":
sound = "Baa";
break;
case "Dog":
case "Cat":
sound = "Woof or Meow";
break;
default:
sound = "Unknown sound";
}
console.log(sound); // Output: Woof or Meow
Example 4: Fall-Through
If you omit the break
statement, the code will continue to execute the following cases until a break
is encountered. This is known as "fall-through."
let grade = 'B';
let message;
switch (grade) {
case 'A':
message = "Excellent";
break;
case 'B':
case 'C':
message = "Well done";
break;
case 'D':
message = "You passed";
break;
case 'F':
message = "Better try again";
break;
default:
message = "Unknown grade";
}
console.log(message); // Output: Well done
In this example, both 'B' and 'C' will result in the message "Well done" because the case 'C':
does not have a break
and thus "falls through" to the case 'B':
block.
Using the switch
statement can make your code cleaner and easier to understand when dealing with multiple possible values for a single variable.
At Online Learner, we're on a mission to ignite a passion for learning and empower individuals to reach their full potential. Founded by a team of dedicated educators and industry experts, our platform is designed to provide accessible and engaging educational resources for learners of all ages and backgrounds.
Copyright 2023-2025 © All rights reserved.