Switch Statements

Use switch for cleaner code when comparing one value against many possible matches.

Step 1 of 4

When if/else chains get too long

Imagine you're building a language selector for a website. The user picks a language code, and you need to display a greeting:

``` if (lang === "en") { ... } else if (lang === "es") { ... } else if (lang === "fr") { ... } else if (lang === "de") { ... } else if (lang === "ja") { ... } else { ... } ```

When you're comparing **one value** against **many possible matches**, this pattern gets repetitive. Every line repeats `lang ===`. The `switch` statement was designed for exactly this scenario — it's cleaner, easier to read, and makes the intent obvious: 'I have one value and several cases to match against.'

Think of it this way: A switch is like a vending machine — you press a button (provide a value), and the machine checks which slot matches to give you the right item. You don't ask 'Is it A? Is it B? Is it C?' one by one — you just look up the slot directly.
Learn more on MDN