JavaScript switch case multiple values | Example code

  • by

You have to use logical OR, AND operator in 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 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 condition 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

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.