Skip to content

JavaScript switch case multiple values | Example code

  • by

You have to use the logical OR, AND operators in the switch case to use multiple values in JavaScript.

JavaScript switch case multiple values

Simple example code test multiple conditions in a JavaScript switch statement. Here is the OR condition, anyone true will execute the case block.

<!DOCTYPE html>
<html>

<body>

  <script>
    let var1 = 2;
    let var2 = 2;

    switch(true) {
      case var1 === 1 || var2 === 1:
      console.log('First case');
      break;

      case var1 === 1 || var2 === 2:
      console.log('Second case');
      break;
    }

  </script>

</body>
</html> 

Output:

JavaScript switch case multiple values

ADN && condition required both conditions true.

<script>
    let var1 = 2;
    let var2 = 2;

    switch(true) {
      case var1 === 1 || var2 === 1:
      console.log('First case');
      break;

      case var1 === 1 && var2 === 2:
      console.log('Second case');
      break;

      default:
      console.log('NONE');
    }

</script>

Output: NONE

Do comment if you have any doubts or suggestions on this JS switch case topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading