How to Use Switch Statements JavaScript

Posted by

A switch statement in JavaScript allows you to test the value of a variable against multiple cases. The basic syntax for a switch statement is as follows:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

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

[/dm_code_snippet]

You can have as many cases as you need. The break statement is used to exit the switch statement once a match is found. If a match is not found, the code in the default block will be executed.

Here is an example of a switch statement that tests the value of a variable called day:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

switch (day) {
    case "Monday":
        console.log("It's Monday!");
        break;
    case "Tuesday":
        console.log("It's Tuesday!");
        break;
    case "Wednesday":
        console.log("It's Wednesday!");
        break;
    default:
        console.log("It's not Monday, Tuesday, or Wednesday.");
}

[/dm_code_snippet]

It’s important to note that JavaScript switch statement compares values with strict equality (===), that means the type of the variable also matters.

In addition to the basic use of switch statements, there are a few other things you can do with them in JavaScript.

  1. Using case labels for multiple values: You can use the same code block for multiple cases by separating the values with a comma. For example:
[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

switch (day) {
    case "Monday":
    case "Tuesday":
    case "Wednesday":
        console.log("It's a weekday!");
        break;
    case "Thursday":
    case "Friday":
    case "Saturday":
    case "Sunday":
        console.log("It's a weekend!");
        break;
    default:
        console.log("Invalid day.");
}

[/dm_code_snippet]

  1. Using expressions in case labels: You can use expressions in case labels, this can be useful when you want to test a variable against a range of values. For example:
[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

switch (true) {
    case (x > 0 && x <= 10):
        console.log("x is between 1 and 10.");
        break;
    case (x > 10 && x <= 20):
        console.log("x is between 11 and 20.");
        break;
    default:
        console.log("x is out of range.");
}

[/dm_code_snippet]

  1. Using switch with let or const: In JavaScript, you can use the let or const keywords to declare variables inside a switch statement. This can be useful when you need to define a variable that will only be used within the switch. For example:
[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

switch (day) {
    case "Monday":
        let message = "It's Monday!";
        console.log(message);
        break;
    case "Tuesday":
        message = "It's Tuesday!";
        console.log(message);
        break;
    default:
        console.log("It's not Monday or Tuesday.");
}

[/dm_code_snippet]

It’s generally considered best practice to use if-else statements instead of switch statements when you have a large number of cases or complex expressions in the case labels. But switch statement can be more efficient and readable than a large series of if-else statements when you have a small number of cases with simple expressions in the case labels.