JavaScript AcademyProfessional Engineering

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

  1. Change day to "sat" and step through. Which cases share code?
  2. Delete one break in the plan program and predict the new output.
  3. Set plan to "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.