7 – Switch statements (Javascript)

Understanding Switch Statements

Switch statements are a fundamental control structure in JavaScript, allowing you to make decisions based on the value of an expression. While if statements are useful for simple true/false conditions, switch statements excel at handling situations where you need to match a single value against multiple possible cases.

The Structure of a Switch Statement

A switch statement consists of the following components:

  • Expression: The value to be compared to different cases.
  • Case Clauses: A set of individual cases that check whether the expression matches their respective values.
  • Default Case: An optional case that executes if none of the preceding cases match the expression.

Here’s the basic structure of a switch statement:


    switch (expression) {
        case value1:
            // Code to execute if expression matches value1
            break;
        case value2:
            // Code to execute if expression matches value2
            break;
        // Additional cases...
        default:
            // Code to execute if no cases match
    }
Using Switch Statements

Switch statements are particularly useful when you have a single expression and need to perform different actions based on its value. Each case checks if the expression matches a specific value. When a match is found, the corresponding code block is executed. If no match is found, the code in the default case, if present, will execute.

Example of a switch statement:


    let day = "Monday";

    switch (day) {
        case "Monday":
            console.log("It's the start of the workweek.");
            break;
        case "Friday":
            console.log("It's almost the weekend!");
            break;
        default:
            console.log("It's a regular day.");
    }

In this example, the value of the variable day is matched against different cases. When it matches “Monday,” the code block for that case executes. If none of the cases match, the default case is executed.

Multiple Cases and Fall-Through

In a switch statement, you can have multiple cases that execute the same code block. When multiple cases share the same code, they can be grouped together, allowing for efficient and organized code.

Example of multiple cases with fall-through:


    let fruit = "apple";

    switch (fruit) {
        case "apple":
        case "pear":
            console.log("It's a type of fruit you can eat.");
            break;
        case "car":
            console.log("It's not a fruit!");
            break;
        default:
            console.log("Not sure what it is.");
    }

In this example, both “apple” and “pear” cases share the same code block. When the fruit variable is either “apple” or “pear,” the code executes, demonstrating fall-through behavior.

Switch vs. If-Else

Switch statements are an excellent choice when you need to compare a single value against multiple cases. However, there are situations where if-else statements may be more appropriate, especially when dealing with complex conditions that don’t rely on direct value matching.

Example of using if-else for complex conditions:


    let score = 85;
    let isBonusQuestion = true;

    if (isBonusQuestion) {
        console.log("Bonus question! Extra points awarded.");
    } else if (score >= 90) {
        console.log("You got an A!");
    } else if (score >= 80) {
        console.log("You got a B.");
    } else {
        console.log("You need to improve your score.");
    }
Conclusion

Switch statements are a powerful tool in JavaScript for making decisions based on the value of an expression. They provide a clear and efficient way to handle multiple cases, offering an alternative to if-else statements when value matching is the primary concern. By mastering switch statements, you can create more organized and readable code that responds intelligently to different conditions.