🧩Switch Statement
Also known as Switch Case, Match Statement, Case Statement
Checks one value against a list of cases and runs the one that matches.
Think of it like
It is like a sorting machine for coins. Each coin size drops into its own slot.
Example
A game reads the key pressed. The switch runs one case for up, one for down and a default for any other key.
Why it matters
Many choices hang on one value, like a menu pick or a status. A switch lays them out in one clear list.
Definition
Example code
javascript
switch (day) {
case "Sat":
case "Sun":
console.log("Weekend");
break;
default:
console.log("School day");
}Common mistake
Key takeaways
- 1One value. Checked against many cases.
- 2Default case. Catches anything not listed.
- 3Mind fall-through. Some languages need break.