JS-048
Visual JavaScript guide to switch: Learn to use switch to select one option from many
Beginner5 min~5 min read#switch#case#control-flow

switch

Many options to choose from

Goal

Learn to use switch to select one option from many.

Explanation

  • switch is used when there are many possible values.
  • Each option is described with a case.
  • JavaScript looks for a case that matches the value.
  • When a match is found, the corresponding code runs.
  • switch often makes code clearer than a long chain of if...else statements.

Analogy

Imagine a drinks menu. You choose one item, and the bartender prepares that specific order.

Code

switch (day) {
  case "Tuesday":
    console.log("Match");
    break;
}

Result: Match

Common mistake

Beginners often forget break. The code may then continue running the following cases.

Remember

  • switch compares a value with each case.
  • The matching case runs.
  • break stops further execution.

Quick Quiz

What is switch used for?

Resources