1. javascript
  2. /basics
  3. /switch

The Switch Statement in JavaScript

Introduction

One of the core features of the language is the ability to control the flow of program execution using conditional statements. We'll delve into the details of the "switch" statement, one of the most powerful and commonly used conditional statements in JavaScript.

You can also check out our Conditional Statements in JavaScript article and refresh your knowledge.

The Basic Syntax of Switch Statements

The basic syntax of a switch statement in JavaScript is as follows:

switch (expression) {
    case value1:
        // code to be executed if expression === value1
        break;
    case value2:
        // code to be executed if expression === value2
        break;
    ...
    default:
        // code to be executed if expression is not === any of the values
}

The switch keyword is followed by an expression in parentheses, which is typically a variable or a function call that returns a value. The expression is then compared to the "case" values, one by one until a match is found. If a match is found, the code block associated with that case is executed. If no match is found, the code block associated with the "default" case is executed (if present).

Moreover, the JavaScript switch statement compares the values using strict equality (===), which means that the data type of the expression and the case values must match.

Here is an example of a simple switch statement that checks the value of a variable "day" and prints a message based on the day of the week:

let day = "Monday";

switch (day) {
    case "Monday":
        console.log("Start of a new work week");
        break;
    case "Wednesday":
        console.log("Middle of the work week");
        break;
    case "Friday":
        console.log("End of the work week");
        break;
    default:
        console.log("Weekend is coming");
}

The default and break Keywords

The default keyword is used to specify a code block that should be executed if none of the case values match the expression. It serves as a catch-all case, when none of the other cases match the expression, the code block associated with the "default" case will be executed.

The break keyword is used to exit a switch statement and prevent the execution of any further code blocks. It's essential to include a break statement after each case block so that the code execution stops and doesn't continue to the next case even if a match has already been found.

Here's a simple example:

let grade = "A";

switch (grade) {
    case "A":
        console.log("Excellent performance");
        break;
    case "B":
        console.log("Good performance");
        break;
    default:
        console.log("Improvement needed");
}

The variable "grade" is assigned the value "A" and the switch statement matches the case "A" and the code block associated with it is executed. Then the break statement is encountered and the switch statement exits. If the "grade" variable is assigned a value that is not "A" or "B" the code block associated with the default case will be executed.

Decision-Making in Conditionals

The switch statement isn't the only way to handle conditional statements in JavaScript. Depending on the situation, the "if-else" statement or a combination of both might be more appropriate.

The "if-else" statement is a more flexible option as it allows for more complex conditions to be evaluated and can handle multiple cases with different code blocks. However, the switch statement is more efficient and easier to read when dealing with a limited number of cases with the same code block.

When to Use the switch Condition

The switch statement is best used when there are multiple stacked "if-else" statements, making the code more readable and efficient.

For example, when determining the price of a product based on its category:

let category = "electronics";
let price = 0;

if (category === "books") {
    price = 20;
} else if (category === "clothes") {
    price = 50;
} else if (category === "electronics") {
    price = 100;
}
console.log(price);

This can be refactored to use a switch statement:

let category = "electronics";
let price = 0;

switch (category) {
    case "books":
        price = 20;
        break;
    case "clothes":
        price = 50;
        break;
    case "electronics":
        price = 100;
        break;
}
console.log(price);

As you can see, the switch statement makes the code more readable and easier to understand. It's clear that the variable category is being checked against the possible values of "books","clothes", and "electronics" followed up by the variable price being assigned the respective value for each case.

Best Practices and Edge Cases

  • Always include a "break" statement after each case, even if it is not strictly necessary. This will prevent unexpected behavior and make the code more readable.

  • Always include a "default" case, even if it is empty, as it makes it clear that the switch statement is intended to handle all possible cases.

  • Avoid using complex expressions or function calls as the expression in a switch statement, as they can make the code harder to read and understand.

  • Be mindful of the data types of the expression and the case values, as the switch statement uses strict equality to compare them.

  • Keep in mind that switch statements can be replaced with other forms of conditional statements when the situation calls for it.

Conclusion

  • The switch statement is best used when there are a limited number of cases with the same code block.

  • It can also be used when there are multiple stacked "if-else" statements, making the code more readable and efficient.

  • By understanding the syntax and best practices of the switch statement, developers can make their code more robust.

A well-written switch statement can make the code more readable and easier to understand, which is crucial for maintaining and scaling any application.