Back to JavaScript Notes
Topic #286

JavaScript switch Statement

You will be able to use a JavaScript switch statement to match one value against several cases and choose the correct branch cleanly.

What it is

A switch statement compares one expression against many possible values. It is like a multi-way branch: JavaScript evaluates the expression, then looks for the first case whose value matches using strict equality (===). If no case matches, it runs the default block if present.

Related terms: case, default, fallthrough, and break. A case is one possible match. Fallthrough means execution continues into the next case when a case does not stop with break, return, or throw.

Why it matters

  • It makes one-variable, many-value logic easier to read than a long chain of if statements.
  • It groups several cases together, such as weekend days, without repeating code.
  • It gives a clear place for unexpected input using default.
  • It helps when values are discrete, such as status codes, menu choices, or day names.

Syntax or steps

  1. Write switch (value) with the expression you want to test.
  2. Add one or more case possibleValue: lines.
  3. Put the code for that case after the colon.
  4. End each case with break;, or use return; if inside a function.
  5. Add default: for values that do not match any case.

Example

function schedule(day) {
  switch (day) {
    case "Sat":
    case "Sun":
      return "rest";
    case "Mon":
    case "Tue":
    case "Wed":
    case "Thu":
    case "Fri":
      return "work";
    default:
      return "unknown";
  }
}

console.log(schedule("Sat")); // "rest"
console.log(schedule("Mon")); // "work"
console.log(schedule("X"));   // "unknown"

The function receives one value, day. The first two cases share the same result: if day is "Sat" or "Sun", the function returns "rest". The next five cases return "work". If the value is not one of the listed strings, default returns "unknown". Because each branch uses return, no break is needed.

Common mistakes

  • Forgetting break or return. The code may fall through to the next case. Fix it by ending each case intentionally.
  • Expecting loose matching. switch uses strict equality, so 1 does not match "1". Fix it by comparing the same type.
  • Declaring variables inside a case without braces. A let or const belongs to the whole switch block and can cause errors. Fix it by wrapping the case body in { }.
  • Leaving out default. Unexpected values may do nothing. Fix it by adding a default branch for logging, errors, or fallback behavior.

When to use it

UseBest whenExample
switchOne value is compared to many exact values.status === "active", "pending", "closed"
if / else ifConditions involve ranges, multiple variables, or complex logic.score >= 90 && level === "advanced"

Use switch for clean discrete matching. Use if when the test is not just “does this value equal one of these values?”

Practice

Guided exercise: Write a function called messageForStatus(status) that returns "Ready" for "active", "Waiting" for "pending", and "Closed" for "closed". Use default to return "Unknown". Expected output: messageForStatus("pending") returns "Waiting".

Challenge: Write a switch that maps a month number from 1 to 12 to a season. Hint: group months with multiple case lines, such as 12, 1, and 2 for winter.

Quick check

What happens if a matching case does not end with break, return, or throw?

JavaScript continues running the next case’s code. This is called fallthrough.

Summary

A switch statement is a compact way to branch on one value against many exact cases. It works best when each case is a clear, discrete match and each branch stops intentionally.

Want to go beyond the notes?

Join Coding Now Tech Institute's JavaScript course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

JavaScript switch Statement – FAQs

Quick answers about learning JavaScript switch Statement in JavaScript.

This free note from Coding Now Tech Institute explains JavaScript switch Statement in JavaScript — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every JavaScript topic on Coding Now Tech Institute, including JavaScript switch Statement, is 100% free with no signup required.
With focused practice, most students grasp JavaScript switch Statement in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now