How to check multiple values in if condition in JavaScript?
Use one/both of the logical operators &&
and ||
the first is read “and”, the second “or” to check the JavaScript Two conditions in if statement.
JavaScript if and 2 conditions example
Let’s see complete HTML examples for Two conditions
And &&
If you want to find the number which is divisible by both 5 and 7, you need to use logical AND, which returns true only if both the conditions are true.
<script>
var x = 35;
if(x%5==0 && x%7==0){
console.log("Print if, both divisible by 5 and 7");
}
</script>
Or ||
If you need to find out, is the number divisible by either 5 or 7?
You need to use logical OR
<script>
var x = 49;
if(x%5==0 || x%7==0){
console.log("Print if, both divisible by 5 and 7");
}
</script>
And && + OR ||
For multiple conditions you have to Wrap them in an extra pair of parents.
<script>
var type = 49;
var count = 1;
if((Type == 2 && count == 0) || (Type == 2 && count == 1)){
console.log("true");
}
</script>
Q: How to check multiple values in if condition in JavaScript?
Answer: Way to Perform Multiple Comparisons in JavaScript:-
<!DOCTYPE html>
<html>
<head>
<script>
var name = 'Jimi';
if (name === 'Jimi' || name === 'Amy' || name === 'Janis') {
alert(" Jimi in List")
}
</script>
</head>
<body>
</body>
</html>
Output:
Do comment if you have any doubts and suggestion on this topic.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version