Skip to content

JavaScript if or multiple conditions

  • by

Use OR (||) operator in the if statement expression to get if or multiple conditions in JavaScript. In expression, if any of the conditions is true, the result is true.

if(id != 1 || id != 2 || id != 3) //this returns true
{
//code
}

The truth table for OR operator:

xyx || y
truetruetrue
truefalsetrue
falsetruetrue
falsefalsefalse

JavaScript if or multiple conditions

A simple example code gets numbers from array values of 20 or less than 10. The || operator short-circuits if the left condition is true.

<!DOCTYPE html>
<html>
<body>
  <script>
   var arr = [5,25,70,62,20,89];

   arr.forEach(num => {
    if (num == 20 || num < 10){
      console.log(num);
    }  
  })

</script>
</body>
</html>

Output:

JavaScript if or multiple conditions

Do comment if you have any doubts or suggestions on this Js condition statement code.

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