How to Set Multiple Conditions in an If Statement in JavaScript?
In JavaScript, conditional statements like if are used to execute code blocks based on certain conditions. There are situations where we might want to check multiple conditions within a single if statement. In such cases, JavaScript provides logical operators like &&
(AND) and ||
(OR) to combine or reverse conditions as well as JavaScript array methods like every()
and some()
for complex evaluations.
Below are the following approaches to set multiple conditions in an if Statement in JavaScript:
Table of Content
Using the AND (&&
) Operator
The AND operator is used when you want all conditions to be evaluated to true
. If any of the conditions isfalse
, the block will not execute.
Syntax:
if (condition1 && condition2 && condition3) {
// Code to execute if all conditions are true
}
Example: In this, we will check if a number is Positive, Even, and Less than 100 and only if all three conditions are true will the message "The number is positive, even, and less than 100"
be printed. Otherwise, the else block will get executed.
let num = 24;
// Define a variable num with a value of 24
// Check if the number is positive, even
// and less than 100
if (num > 0 && num % 2 === 0 && num < 100) {
// All conditions are true, so this
//block will execute
console.log(`The number is positive, even and less than 100`);
} else {
// If any condition is false,
//this block will execute
console.log(`The number doesn't meet all the conditions`);
}
Output
The number is positive, even and less than 100
Using the OR (||
) Operator
The OR operator is used when you want at least one of the conditions to be true
. The block will execute if any condition is satisfied.
Syntax:
if (condition1 || condition2 || condition3) {
// Code to execute if any of the conditions are true
}
Example: Here we check if the number is either Negative or Greater than 100.If either of these conditions is true, the message "The number is either negative or greater than 100"
is printed.
let num = 150;
// Define a variable num with a value of 150
// Check if the number is either
//negative or greater than 100
if (num < 0 || num > 100) {
// At least one of the conditions is true,
// this block will execute
console.log("The number is either negative or greater than 100");
} else {
// If neither condition is true,
//this block will execute
console.log("The number doesn't meet any of the conditions");
}
Output
The number is either negative or greater than 100
Combining Multiple Operators
We can also combine AND and OR operators for more complex logic. Parentheses can be used to group conditions and specify the order of evaluation.
Syntax:
if ((condition1 && condition2) || (!condition3 && condition4)) {
// Code to execute based on combined conditions
Example: Here we combine both AND (&&
) and OR (||
) operators to check whether if a number is Positive and even OR not divisible by 5. If either condition is true, it will print "The number meets the complex condition"
.
let num = 22;
// Define a variable num with a value of 22
// Check if the number is positive and even,
// or not divisible by 5
if ((num > 0 && num % 2 === 0) || num % 5 !== 0) {
// At least one condition is true,
// so this block will execute
console.log("The number meets the complex condition");
} else {
// If none of the conditions are true,
// this block will execute
console.log("The number doesn't meet the complex condition");
}
Output
The number meets the complex condition
Using the every()
Method
The every()
method is used to check if all elements of an array satisfy a certain condition. It returns true
only if all elements pass the test.
Syntax:
array.every(callback(element, index, array))
Example: Here, we created an array of conditions and used every () method
to check if all conditions are true
.
let conditions = [10 > 5, 20 > 10, 30 > 20]; // Array of conditions
// Use every() to check if all conditions are true
if (conditions.every(Boolean)) {
console.log("All conditions are true");
} else {
console.log("Not all conditions are true");
}
Output
All conditions are true
Using the some()
Method
The some()
method checks if at least one element in an array satisfies a certain condition. It returns true
if any element passes the test.
Syntax:
array.some(callback(element, index, array))
Example: Here, we used the some()
method to check if at least one condition in the array is true.
let conditions = [10 > 15, 20 > 25, 30 > 25]; // Array of conditions
// Use some() to check if at least one condition is true
if (conditions.some(Boolean)) {
console.log("At least one condition is true");
} else {
console.log("None of the conditions are true");
}
Output
At least one condition is true