«Back to Home

Learn JavaScript

Topics

switch Case in JavaScript

The switch case statement is another way to handle multiple conditions. It is useful when you want to compare one value with many possible matching values. Instead of writing several if and else if statements, you can use switch to make your code cleaner and easier to read.

College students and freshers often find switch helpful for menu-driven programs, grade systems, and simple decision-making applications.

Why Use switch Case?

Use switch when:

  • One value needs to be compared with many fixed options

  • You want clean and organized code

  • You want to avoid long chains of else if

  • Your conditions depend on exact matches

Examples where switch is useful:

  • Choosing a day of the week

  • Selecting a menu option

  • Checking grades

  • Handling user choices

Basic switch Syntax

switch (value) {
    case option1:
        // code
        break;

    case option2:
        // code
        break;

    default:
        // code
}

Important parts:

  • value ? the variable to compare

  • case ? each possible match

  • break ? stops the switch block

  • default ? runs when no case matches

Example 1: Day of the Week

let day = 3;

switch (day) {
    case 1:
        console.log("Monday");
        break;

    case 2:
        console.log("Tuesday");
        break;

    case 3:
        console.log("Wednesday");
        break;

    case 4:
        console.log("Thursday");
        break;

    case 5:
        console.log("Friday");
        break;

    default:
        console.log("Weekend");
}

Output:

Wednesday

Why break Is Important

If you skip break, JavaScript will continue running the next case.
This is called fall-through.

Example:

let num = 2;

switch (num) {
    case 1:
        console.log("One");

    case 2:
        console.log("Two");

    case 3:
        console.log("Three");

    default:
        console.log("Default case");
}

Output:

Two
Three
Default case

Why does this happen?
Because there are no breaks, so the program continues executing all cases after the match.

Example 2: Grade System

let grade = "B";

switch (grade) {
    case "A":
        console.log("Excellent");
        break;

    case "B":
        console.log("Very Good");
        break;

    case "C":
        console.log("Good");
        break;

    case "D":
        console.log("Pass");
        break;

    default:
        console.log("Invalid Grade");
}

Output:

Very Good

Example 3: Menu Selection

let choice = 2;

switch (choice) {
    case 1:
        console.log("Start Game");
        break;

    case 2:
        console.log("Load Game");
        break;

    case 3:
        console.log("Exit");
        break;

    default:
        console.log("Invalid Choice");
}

Output:

Load Game

Using Multiple Cases for One Output

You can combine cases if they have the same result.

let fruit = "apple";

switch (fruit) {
    case "apple":
    case "mango":
    case "banana":
        console.log("This is a fruit");
        break;

    default:
        console.log("Unknown item");
}

Output:

This is a fruit

This works because the program reaches the matching case and then falls through until it finds the first console.log.

Example Program With Output (Real-Life Use)

let paymentMethod = "UPI";

switch (paymentMethod) {
    case "Cash":
        console.log("You selected Cash");
        break;

    case "Card":
        console.log("You selected Card Payment");
        break;

    case "UPI":
        console.log("You selected UPI Payment");
        break;

    default:
        console.log("Please select a valid payment method");
}

Output:

You selected UPI Payment

When Not to Use Switch

Avoid using switch when:

  • Conditions involve ranges (example: marks >= 50)

  • Logic is too complex

  • You need calculations

  • You can use objects or functions instead

For multiple-value matches, the switch is clean and precise.

Practice Tasks (Do It Yourself)

  1. Write a switch program to print the month name for numbers 1–12.

  2. Create a menu for a simple calculator (1 = Add, 2 = Subtract, etc.).

  3. Use a switch to check the type of vehicle (car, bike, truck).

  4. Use switch to print greetings based on time (Morning, Afternoon, Evening).

  5. Write a program that checks semesters like “1st Sem”, “2nd Sem”, “3rd Sem”, etc.

Author
Ankur Mishra
197 9.6k 1.4m