ProgramGuru

switch Statement

Learn switch Statement

A switch chooses a matching branch based on the value of one expression.

In this lesson, you will study the rule, run a complete TypeScript example, explain its output, and test a small change.

What you’ll learn

  • Explain the main rule behind switch statement.
  • Predict the result of the complete runnable TypeScript example.
  • Recognize one common mistake and correct it.

Understand the concept

JavaScript switch compares case labels using strict matching semantics. Use break to prevent fallthrough to the next case unless fallthrough is intentional. The default branch handles values with no matching case. TypeScript can also narrow discriminated unions inside switch branches.

See the concept visually

Use the diagram to locate the checks, values or branches involved in the example.

switch Statement: illustrated flowEvaluate day = 3; Match case 3; Print Wednesday; break → exit1Evaluate day = 32Match case 33Print Wednesday4break → exitFollow the change from source values to the final result.
switch Statement: illustrated flow. Evaluate day = 3; Match case 3; Print Wednesday; break → exit.

Run a complete TypeScript example

Save this code as main.ts or paste it into the TypeScript Playground. The example uses fixed values so it runs without prompting for input.

TypeScript code

const day: number = 3;
 switch (day) {
   case 1: console.log("Monday"); break;
   case 2: console.log("Tuesday"); break;
   case 3: console.log("Wednesday"); break;
   default: console.log("Other day");
 }

Verified output

Wednesday

How to run locally: npx tsc --strict --target es2022 --module commonjs main.ts, then node main.js. When using a project configuration, run the matching project build instead.

Trace the execution

The switch expression evaluates to 3, so case 3 matches and prints Wednesday. break exits the switch immediately.

Predict the output before you run the sample. Compare each line with the source expression that produced it, and distinguish the type checker’s work from what happens in JavaScript at runtime.

Common mistakes and how to avoid them

Do not forget break if you do not intend multiple case bodies to run. The case string "3" would not strictly match the number 3.

Debugging habit: when a change is rejected, read the exact compiler diagnostic and inspect the operand types. When it compiles but behaves unexpectedly, follow the values through the JavaScript expressions.

Practice it yourself

Add case 4 and test day values 4 and 10. Then deliberately remove one break to observe fallthrough.

Challenge: write down your prediction first, run the changed source, and explain any difference. Return to the verified version before continuing.

Check your understanding

Why is break common inside switch cases?
Choose one answer. You can try again.

Answer and explanation

It prevents unintended fallthrough into following cases.

break exits the nearest switch or loop.

Review the key idea

switch Statement: JavaScript switch compares case labels using strict matching semantics.

break exits the nearest switch or loop.

Before continuing, be able to explain what is checked before execution and what actually changes at runtime in the example above.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: for Loop →