Learning object 10 of 100
Switch statement visualizer
Step through switch statements and watch which case matches, where execution jumps, and how break stops it falling through into the next case.
Loading…
What it shows
switch compares its value with each case using strict equality. Execution starts at the first matching case and continues downward until a break (or the end). Leaving out break makes the next case run too, which is occasionally useful for grouping cases and usually a bug.
Guided experiment
- Change
dayto "sat" and step through. Which cases share code? - Delete one
breakin the plan program and predict the new output. - Set
planto "gold". Which branch runs?
Predict
In a switch on "pro" where case "pro" has no break and is followed by case "team", what runs?
Related
Lessons: Lesson 14: Switch statements and conditional expressions
Projects: Project 9: Shopping Cart Price Calculator
Source: web/assets/js/objects/object-010.js in the companion repository.