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:
x | y | x || y |
true | true | true |
true | false | true |
false | true | true |
false | false | false |
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:
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