Lesson 37 of 48
77%
Switch Statements
switch picks a path based on one value.
It shines when you have many exact matches.
Think of a vending machine. Each button code opens one snack door.
1
Basic switch
2
case and break
3
default
4
Fall through
5
When to use switch
Basic shape
Write switch (value) then list cases.
javascript
case
Each case checks for one exact match.
It uses strict matching, like ===.
javascript
break
break leaves the switch.
Without break, code can fall into the next case.
javascript
default
default runs when no case matches.
It is the else of a switch.
javascript
Fall through on purpose
Sometimes several cases should share the same code.
Stack the cases with no break between them.
javascript
Tip: Use switch for many exact values. Use if else for ranges like score >= 90.
Test yourself
Three quick questions made just for this lesson. Earn 10 XP per correct answer.