How to Use Switch Case with Multiple Numbers in JavaScript?

Estimated read time 1 min read

You can use a switch statement with multiple values for a single case. This is useful when you want to perform the same action for multiple values.

Here is an example:

var number = 3;

switch (number) {
  case 1:
  case 2:
  case 3:
    console.log("The number is between 1 and 3.");
    break;
  case 4:
  case 5:
  case 6:
    console.log("The number is between 4 and 6.");
    break;
  default:
    console.log("The number is not between 1 and 6.");
}

In this example, the variable number is evaluated. If number is equal to 1, 2, or 3, the code console.log("The number is between 1 and 3.") is executed, and the switch statement is exited. If number is equal to 4, 5, or 6, the code console.log("The number is between 4 and 6.") is executed, and the switch statement is exited. If number does not match any of the cases, the code in the default case is executed.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply