JavaScript Switch Case Statement
When you need to compare one value against many fixed options, the switch statement is cleaner than a long if-else-if chain. It also makes the code easier to read.
What is Switch Case?
switch is a control statement that checks a value against a list of case options. When a case matches, that block runs. The default case runs if no case matches.
Why Use Switch Case?
Cleaner Code
Better than a chain of else-if when comparing one value.
Faster Reading
Cases sit one below the other — easy to scan.
Strict Match
Uses === (strict equality) for comparison.
break
Ends a case and exits the switch block.
Fall-through
Without break, the next case also runs — useful for groups.
default
Safety net when no case matches.
Basic Features
- Switch compares using
===, so types must match. breakis needed after each case unless intentional fall-through.- Multiple cases can share the same code by stacking them without break.
defaultcan appear anywhere but is usually last.
Switch Case in JavaScript
A switch statement in JavaScript is a control flow statement used to execute different blocks of code based on multiple possible conditions. It is cleaner and more organized than writing many if-else statements.
What is Switch Case ?
The switch statement checks the value of an expression and matches it with different case values. If a match is found, the corresponding code block executes.
- Used for multiple conditions.
- Improves readability.
- Cleaner than long if-else chains.
- Supports default conditions.
Structure of Switch Case
switch(expression){
case value1:
// statements
break;
case value2:
// statements
break;
default:
// default statements
}
Why Switch Case is Used ?
- Makes code easier to read.
- Better organization of conditions.
- Faster for many comparisons.
- Supports default fallback option.
- Easier maintenance of code.
Your First Switch Program
<script>
let day = 3;
switch(day){
case 1:
document.write(
"Monday"
);
break;
case 2:
document.write(
"Tuesday"
);
break;
case 3:
document.write(
"Wednesday"
);
break;
default:
document.write(
"Weekend"
);
}
</script>
Example 1 : Fruit Taste
<script>
var fruit = "banana";
var taste;
switch(fruit){
case "banana":
taste = "sweet";
break;
case "lemon":
taste = "sour";
break;
case "apple":
taste = "crisp";
break;
default:
taste = "unknown";
}
document.write(
"The taste of "
+ fruit +
" is "
+ taste
);
</script>
Example 2 : Weekday Program
<script>
var ch = 2;
switch(ch){
case 1:
document.write(
"Sunday"
);
break;
case 2:
document.write(
"Monday"
);
break;
case 3:
document.write(
"Tuesday"
);
break;
default:
document.write(
"Invalid Choice"
);
}
</script>
Example 3 : Calculator
<script>
let a = 10;
let b = 5;
let op = "*";
switch(op){
case "+":
document.write(
a+b
);
break;
case "-":
document.write(
a-b
);
break;
case "*":
document.write(
a*b
);
break;
case "/":
document.write(
a/b
);
break;
default:
document.write(
"Invalid"
);
}
</script>
Example 4 : Grade System
<script>
let marks = 78;
let grade =
Math.floor(
marks / 10
);
switch(grade){
case 10:
case 9:
document.write(
"A+"
);
break;
case 8:
document.write(
"A"
);
break;
case 7:
document.write(
"B"
);
break;
default:
document.write(
"Fail"
);
}
</script>
Example 5 : Vowel or Consonant
<script>
let ch = "e";
switch(ch){
case "a":
case "e":
case "i":
case "o":
case "u":
document.write(
ch +
" is vowel"
);
break;
default:
document.write(
ch +
" is consonant"
);
}
</script>
Example 6 : VIBGYOR Colours
<script>
var ch = "g";
switch(ch){
case "v":
document.write(
"violet"
);
break;
case "g":
document.write(
"green"
);
break;
case "r":
document.write(
"red"
);
break;
default:
document.write(
"Invalid"
);
}
</script>
Important Notes
- Always use break statements.
- switch uses strict equality.
- default is optional but useful.
- Multiple cases can share one block.
Real-Life Use Cases
Menu Routing
Route users to different pages.
Order Status
Show delivery status updates.
Game Levels
Change effects based on level.
Frequently Asked Questions
| Question | Answer |
|---|---|
| Can switch compare strings? | Yes, strings and numbers both work. |
| What happens without break? | Execution falls to next case. |
| Is default necessary? | No, but recommended. |
Conclusion
The switch statement is a clean and powerful way to compare one value against multiple conditions. It improves readability, structure, and maintainability of JavaScript code.
JavaScript All Chapters
Continue Learning
Previous
Go to Math Function Chapter