JS Switch
switch (expression) {
case value1:
// code
break;
default:
// code
}
Basic switch Syntax
A switch statement takes a value in parentheses and compares it against each case label in order, running the code inside the first matching case block.
उदाहरण: Basic switch Syntax
// Declare the constant `day`, set to "Mon"
// Declare the constant `day`, set to "Mon"
const day = "Mon";
// Switch on `day` and branch by matching case
// Switch on `day` and branch by matching case
switch (day) {
// Case: `"Mon"`
// Case: `"Mon"`
case "Mon":
// Print "Start of week" to the console
// Print "Start of week" to the console
console.log("Start of week");
}
case and break
Each case label marks the start of a block to run for a specific matching value, and break exits the switch statement immediately once its block finishes, preventing execution from continuing into the next case.
उदाहरण: case and break
// Declare the constant `day`, set to "Mon"
// Declare the constant `day`, set to "Mon"
const day = "Mon";
// Switch on `day` and branch by matching case
// Switch on `day` and branch by matching case
switch (day) {
// Case: `"Mon"`
// Case: `"Mon"`
case "Mon":
// Print "Monday" to the console
// Print "Monday" to the console
console.log("Monday");
break;
// Case: `"Tue"`
// Case: `"Tue"`
case "Tue":
// Print "Tuesday" to the console
// Print "Tuesday" to the console
console.log("Tuesday");
break;
}
default
The default case runs when none of the listed case labels match the switch value, functioning similarly to a final else in an if/else if chain, and is commonly placed last, though it can technically appear anywhere.
उदाहरण: default
// Declare the constant `day`, set to "Sun"
// Declare the constant `day`, set to "Sun"
const day = "Sun";
// Switch on `day` and branch by matching case
// Switch on `day` and branch by matching case
switch (day) {
// Case: `"Mon"`
// Case: `"Mon"`
case "Mon":
// Print "Weekday" to the console
// Print "Weekday" to the console
console.log("Weekday");
break;
// Default case, if nothing above matched
// Default case, if nothing above matched
default:
// Print "Not Monday" to the console
// Print "Not Monday" to the console
console.log("Not Monday");
}
Grouping Cases Together
Multiple case labels can share the same block of code by stacking them without a break in between, letting several different values all trigger the same response.
उदाहरण: Grouping Cases Together
// Declare the constant `day`, set to "Sat"
// Declare the constant `day`, set to "Sat"
const day = "Sat";
// Switch on `day` and branch by matching case
// Switch on `day` and branch by matching case
switch (day) {
// Case: `"Sat"`
// Case: `"Sat"`
case "Sat":
// Case: `"Sun"`
// Case: `"Sun"`
case "Sun":
// Print "Weekend" to the console
// Print "Weekend" to the console
console.log("Weekend");
break;
// Default case, if nothing above matched
// Default case, if nothing above matched
default:
// Print "Weekday" to the console
// Print "Weekday" to the console
console.log("Weekday");
}
switch vs if-else
switch tends to be clearer when comparing one single value against many specific possibilities, while if-else chains are more flexible for complex conditions involving ranges, multiple variables, or logical combinations.
उदाहरण: switch vs if-else
const num = 5;
// switch is clean for one value vs many options:
switch (num) {
case 5:
console.log("Five");
break;
}
// if-else is better for ranges:
if (num > 0 && num < 10) {
console.log("Single digit");
}
Chapter Quiz — Complete all 26 topics to unlock
0/26 topics done
Complete these topics first:
- JS Dates
- JS Math
- JS Conditionals
- JS Switch
- JS Loop For
- JS Loop While
- JS Iterables
- JS Sets
- JS Maps
- JS typeof
- JS Type Conversion
- JS Destructuring
- JS Arrow Functions
- JS Classes
- JS Modules
- JS Promises
- JS Async/Await
- JS DOM
- JS DOM Methods
- JS Events Advanced
- JS DOM Navigation
- JS DOM Collections
- JS Async Callbacks
- JS Async Parallel
- JS Date Set
- JS Set Logic