Skip to content
dreamcode
dreamcode
Map
Switch
Lesson 11 of 48
+15 XP on finish
JS LOGICChapter 2 · JS Conditionals & Logic

Switch Statements

A switch compares one value against several case labels using === and runs the matching block. End each case with break, or execution falls through into the next case. A default case runs when nothing matches. Stacking two cases with no code between them lets both share one block.

Worked example

How it reads

  • Each case is compared with === against phase
  • break stops the switch after the matching block
  • default catches every value no case matched
Common mistakes
  • Forgetting break makes the code fall through and run the next case too.
Cloud tip: A switch reads well when you compare one value against many fixed options. For ranges like score > 90, use if and else if.
index.js
JAVASCRIPT
real JavaScript, runs in your browser
Console
Run your code to see its output here.
YOUR TURN

Add cases so both "sat" and "sun" print Weekend. Stack the two cases so they share one block.

Press Run to check your work.