Javascript Switch Multiple Case


In this tutorial, you will learn how to match multiple cases in a switch statement in javascript.

Switch Statement

The switch statement in JavaScript is used to perform different actions based on different conditions. It is similar to the if-else statement. The switch statement is often used together with a break or a default keyword.

The switch statement uses an expression to match the value of a variable. The case keyword is used to compare the value of the variable with the value of the expression.

If the value matches, the code inside the case block is executed. If the value does not match, the code inside the default block is executed.

Here is an example of a switch statement in JavaScript.

Example

// switch statement
var age = 18;
switch (age) {
  case age < 18:
    console.log("You are young");
    break;
  case age >= 18:
    console.log("You are adult");
    break;
  default:
    console.log("You are old");
}

JavaScript switch multiple case

Multiple Cases in Switch Statement

Occasionally, we need to match expressions for multiple cases in a switch statement. We can do it in multiple different ways in JavaScript.

Here are 4 different ways to match multiple cases in a switch statement in JavaScript.


1. Using Fallthrough Technique

The fallthrough technique most commonly used technique to match multiple cases in a switch statement in JavaScript.

In this technique, we just create multiple cases with and only the last case has a code block and break statement.

Here is an example of fallthrough technique in switch statement.

Example

// fallthrough technique
var name = "Jane";

switch (name) {
  case "John":
  // fall through
  case "Jane":
  // fall through
  case "Jack":
    console.log("Hello " + name);
    break;
  default:
    console.log("Hello Stranger");
}

2. Writing Multiple Cases in a Single Line

We can also write multiple cases in a single line in a switch statement in JavaScript.

And that too works the same as the fallthrough technique.

Example

// multiple cases in a single line
var name = "Jane";

switch (name) {
  case "John": case "Jane": case "Jack":
    console.log("Hello " + name);
    break;
  default:
    console.log("Hello Stranger");
}

3. Using Includes Method

The includes() method is used to check if an array contains a specified element.

Here is an example of the includes method in JavaScript.

Example

// includes method
var name = "Jane";

switch (true) {
  case ["John", "Jane", "Jack"].includes(name):
    console.log("Hello " + name);
    break;
  default:
    console.log("Hello Stranger");
}

4. Using Array IndexOf Method

The indexOf() method is used to search an array for a specified element and returns its position. If the element is not found, it returns -1.

Here is an example of indexOf method in JavaScript.

Example

// indexOf method
var name = "Jane";

switch (true) {
  case ["John", "Jane", "Jack"].indexOf(name) > -1:
    console.log("Hello " + name);
    break;
  default:
    console.log("Hello Stranger");
}

Conclusion

We have learned 4 techniques to handle multiple cases in a switch statement in JavaScript.

This is the end of the tutorial. Don't stop learning. Keep learning and keep coding. Explore more from the sidebar section.