How to Use Switch Statements in JavaScript
Use switch when one value can lead to several exact outcomes. This is cleaner than long if...else if chains when you are matching a single variable against multiple known cases.
What you’ll build or solve
You’ll learn how to use switch statements in JavaScript with case, break, and default. You’ll also know when switch reads better than chained conditionals.
Learn JavaScript on Mimo
When this approach works best
This approach is the right choice when one variable maps to several exact values.
Common real-world scenarios include:
- Status messages
- Theme modes
- User roles
- Keyboard shortcuts
- Route-based UI rendering
This is a bad idea when each branch depends on different boolean logic. In that case, if statements stay clearer.
Prerequisites
You only need:
- A JavaScript file or browser console
- Basic variables and conditionals knowledge
Step-by-step instructions
Step 1: Match one value against multiple cases
Start with the variable inside switch.
JavaScript
const role = "admin";
switch (role) {
case "admin":
console.log("Full access");
break;
case "editor":
console.log("Edit access");
break;
default:
console.log("Read only");
}
Each case checks one exact match.
Use default as the fallback branch.
This pattern is ideal for enums, status labels, and UI modes.
What to look for:
switchchecks one variablecasevalues are exact matchesbreakstops fall-throughdefaulthandles unmatched values- Cleaner than long exact-match
ifchains
Examples you can copy
Theme mode
JavaScript
switch (theme) {
case "dark":
enableDarkMode();
break;
case "light":
enableLightMode();
break;
}
User role
JavaScript
switch (user.role) {
case "pro":
showPremium();
break;
}
Status badge
JavaScript
switch (status) {
case "success":
badge.textContent = "Done";
break;
}
Common mistakes and how to fix them
Mistake 1: Forgetting break
What the reader might do:
JavaScript
switch (role) {
case "admin":
console.log("Admin");
case "editor":
console.log("Editor");
}
Why it breaks: execution continues into the next case.
Corrected approach:
JavaScript
case "admin":
console.log("Admin");
break;
Mistake 2: Using expressions instead of exact matches
What the reader might do:
JavaScript
switch (score > 80)
Why it breaks: switch is best for exact values, not varied boolean logic.
Corrected approach:
Use if for condition ranges.
Mistake 3: Forgetting default
What the reader might do:
JavaScript
switch (theme) {
case "dark":
enableDarkMode();
break;
}
Why it breaks: unmatched values silently do nothing.
Corrected approach:
JavaScript
default:
enableLightMode();
Troubleshooting
If multiple cases run, add missing break statements.
If no branch matches, confirm the variable value exactly matches the case string.
If conditions are range-based, switch back to if.
If unexpected values appear, add a default fallback.
Quick recap
- Use
switchfor one exact-match variable - Add
casefor each possible value - Use
breakto stop fall-through - Use
defaultas fallback - Prefer
iffor range logic
Join 35M+ people learning for free on Mimo
4.8 out of 5 across 1M+ reviews
Check us out on Apple AppStore, Google Play Store, and Trustpilot