Switch Statements
if/else if chains work well for a handful of conditions, but when you have many specific cases based on the same value, switch statements can make your code cleaner and easier to read. Today you'll learn how to use switch, where it shines, and when to stick with if.
Step 1: Basic Structure
const level = "gold";
switch (level) {
case "bronze":
console.log("Bronze member perks unlocked.");
break;
case "silver":
console.log("Silver perks unlocked. Keep it up!");
break;
case "gold":
console.log("Gold perks unlocked. You're amazing! ✨");
break;
default:
console.log("Set your membership level to see perks.");
}switchevaluates the expression once (levelin this example).- It compares the result to each
casevalue using strict equality (===). - When it finds a match, it runs the code until a
breakstatement or the end of the switch. defaultruns if no cases match.
Step 2: Remember the break
Without break, execution falls through to the next case:
const day = "Monday";
switch (day) {
case "Monday":
console.log("Start of the week!");
case "Tuesday":
console.log("Keep the momentum.");
default:
console.log("Another opportunity to learn!");
}Output:
Start of the week!
Keep the momentum.
Another opportunity to learn!To prevent accidental fall-through, add break; at the end of each case unless you intentionally want to fall through.
Step 3: Intentional Fall-Through
Sometimes you want multiple cases to share the same outcome:
const mood = "excited";
switch (mood) {
case "happy":
case "excited":
case "motivated":
console.log("Channel that energy into your code! 💥");
break;
case "tired":
console.log("Take a stretch break and hydrate.");
break;
default:
console.log("Check in with yourself and keep learning!");
}- Group cases without code in between to intentionally fall through to the shared block.
Step 4: Using Expressions in Cases
Switch cases can be expressions, but they're evaluated once at compile time. For dynamic checks, if statements are better.
Still, you can mix logic inside the case body:
const points = 87;
switch (true) {
case points >= 100:
console.log("Elite tier unlocked!");
break;
case points >= 80:
console.log("Great progress—almost elite!");
break;
default:
console.log("Keep stacking those points!");
}- Here,
switch(true)allows each case to evaluate a boolean expression. Use this pattern sparingly; a standardif/else ifmay be clearer.
Step 5: Comparing if vs switch
Use switch when:
- You're checking the same value against many possibilities.
- The cases are discrete and descriptive (strings, numbers, enums).
- You want a tidy list of outcomes without repeated
if (...) { ... } else if (...) { ... }.
Stick with if/else when:
- Conditions depend on different expressions or ranges.
- You need complex logic inside conditions.
- You rely on less/greater-than comparisons or compound logic (
x > 10 && y < 5).
Example of a concise switch:
const command = "share";
switch (command) {
case "start":
console.log("Lesson started.");
break;
case "pause":
console.log("Lesson paused.");
break;
case "share":
console.log("Progress shared with friends!");
break;
default:
console.log("Unknown command.");
}Step 6: Practice Prompts
- Create a
switchforconst language = "tr";that logs a greeting in English, Turkish, or Spanish. Usedefaultfor unsupported languages. - Group multiple cases:
const dayType = "weekend";where"saturday"and"sunday"share the same block. - Use
switch(true)to classifyconst minutes = 52;into"Short session","Focused block", or"Marathon coder"based on ranges. - Compare the readability of your solution with an equivalent
if/else if. Which do you prefer and why?
Key Takeaways
- ✅
switchis great for choosing between many fixed options based on the same value. - ✅ Always include
breakunless you intentionally fall through. - ✅ Group cases to handle shared outcomes without repeating code.
- ✅
if/elseremains better for ranges and complex conditions.
🎯 Quick Check
- What happens if you forget
breakin aswitchcase? - How do you handle multiple cases that share the same outcome?
- When might
switch(true)be useful? - Give one scenario where
if/elseis more appropriate thanswitch.
Great job! You're ready for a hands-on challenge next—the Day 2 calculator project awaits. 🧮