JavaScript Switch Statement

Learn JavaScript switch statements with detailed explanations, syntax, examples, outputs, and real-world practical usage.

JavaScript Switch Statement

The switch statement is used to execute one block of code from multiple possible conditions. It is an alternative to writing many if else statements.

Switch statements make programs cleaner and easier to read when checking multiple values.

Why Switch Statement is Important?

  • Handles multiple conditions easily
  • Makes code cleaner and readable
  • Improves program organization
  • Useful for menus and selections
  • Reduces long if else chains

Syntax of Switch Statement

switch(expression){

    case value1:
        code block;
        break;

    case value2:
        code block;
        break;

    default:
        code block;

}

Explanation of Syntax

  • switch(expression) checks a value.
  • case defines possible matching values.
  • break stops execution after a match.
  • default executes when no case matches.

Basic Switch Example

The following example checks a day number.

let day = 2;

switch(day){

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

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

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

    default:
        console.log("Invalid Day");

}

Output

Tuesday

Detailed Explanation

  • The value of day is 2.
  • JavaScript checks each case one by one.
  • case 2 matches the value.
  • "Tuesday" gets printed.
  • break stops further execution.

Importance of break Statement

The break statement stops the switch execution after a match is found.

let number = 1;

switch(number){

    case 1:
        console.log("One");

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

}

Output

One

Two

Explanation

  • Without break, execution continues to the next case.
  • This behavior is called fall-through.

Using default Case

The default block executes when no case matches.

let color = "Green";

switch(color){

    case "Red":
        console.log("Stop");
        break;

    case "Yellow":
        console.log("Wait");
        break;

    default:
        console.log("Go");

}

Output

Go

Explanation

  • No case matches the value "Green".
  • The default block executes.

Multiple Cases with Same Output

Multiple cases can share the same code block.

let fruit = "Apple";

switch(fruit){

    case "Apple":

    case "Mango":

    case "Banana":

        console.log("Fruit Available");

        break;

    default:

        console.log("Fruit Not Available");

}

Output

Fruit Available

Explanation

  • All matching fruits share the same output.
  • This reduces duplicate code.

Switch Uses Strict Comparison

Switch statements use strict comparison (===).

let value = "10";

switch(value){

    case 10:
        console.log("Number");

        break;

    default:
        console.log("No Match");

}

Output

No Match

Explanation

  • "10" is a string.
  • 10 is a number.
  • Strict comparison checks both value and datatype.

Nested Switch Statement

A switch statement can exist inside another switch statement.

let category = "Electronics";

let item = "Mobile";

switch(category){

    case "Electronics":

        switch(item){

            case "Mobile":

                console.log("Mobile Selected");

                break;

        }

        break;

}

Output

Mobile Selected

Complete Real Life Example

The following example shows a simple restaurant menu system using JavaScript switch statement.

<!DOCTYPE html>

<html>

<head>

    <title>JavaScript Switch Example</title>

</head>

<body>

<h2>Restaurant Menu</h2>

<input type="text"
id="food"
placeholder="Enter Food Item">

<br><br>

<button onclick="checkFood()">

Check Availability

</button>

<h3 id="output"></h3>

<script>

function checkFood(){

    let item =
    document.getElementById("food").value;

    switch(item){

        case "Pizza":

            document.getElementById("output").innerHTML =

            "Pizza is Available";

            break;

        case "Burger":

            document.getElementById("output").innerHTML =

            "Burger is Available";

            break;

        case "Pasta":

            document.getElementById("output").innerHTML =

            "Pasta is Available";

            break;

        default:

            document.getElementById("output").innerHTML =

            "Item Not Available";

    }

}

</script>

</body>

</html>

Output

Restaurant Menu

Input:
Pizza

Output:
Pizza is Available

Detailed Explanation of Example

  • The input field accepts food names.
  • The button executes checkFood() function.
  • switch(item) checks the entered value.
  • If a matching case is found, related output displays.
  • break prevents further execution.
  • default executes when no item matches.
  • innerHTML updates webpage content dynamically.

Switch vs If Else

  • if else is better for complex conditions.
  • switch is better for checking fixed values.
  • switch improves readability for multiple options.
  • Both are used for decision making.

Common Mistakes

  • Forgetting break statements.
  • Using wrong datatype in cases.
  • Ignoring default block.
  • Writing duplicate cases.

Best Practices

  • Always use break when required.
  • Use meaningful case values.
  • Keep switch statements clean and readable.
  • Use default for unexpected values.
  • Prefer switch for fixed value comparisons.

Important Notes

  • switch checks multiple conditions efficiently.
  • break stops execution after a match.
  • default handles unmatched cases.
  • switch uses strict comparison.
  • Switch statements improve readability.

Summary

  • switch is used for multiple conditions.
  • case defines matching values.
  • break prevents fall-through behavior.
  • default handles unmatched values.
  • switch uses strict comparison.
  • Switch statements simplify complex decision making.