how to put multiple conditions in if statement node .js
- Published
- Reading time
- 1 min read
- Length
- 277 words
On this page
How to Put Multiple Conditions in If Statement in Node.js#
When working with Node.js, there may be times when you need to check multiple conditions in an if statement. This can be done using logical operators such as "&&" and "||". Here are a few ways to accomplish this:
Using the && operator#
The && operator is used to check if all conditions are true. If all conditions are true, then the if statement will execute. Here's an example:
if (num1 > 0 && num2 < 10) {
// Both conditions are true
console.log("Both conditions are true.");
}In the above example, the if statement will only execute if num1 is greater than 0 AND num2 is less than 10.
Using the || operator#
The || operator is used to check if at least one condition is true. If at least one condition is true, then the if statement will execute. Here's an example:
if (num1 > 0 || num2 < 10) {
// At least one condition is true
console.log("At least one condition is true.");
}In the above example, the if statement will execute if num1 is greater than 0 OR num2 is less than 10.
Using nested if statements#
If you need to check multiple conditions that are more complex, you can use nested if statements. Here's an example:
if (num1 > 0) {
if (num2 < 10) {
// Both conditions are true
console.log("Both conditions are true.");
}
}In the above example, the inner if statement will only execute if num1 is greater than 0 AND num2 is less than 10.
These are just a few ways to put multiple conditions in an if statement in Node.js. Choose the method that best fits your needs and coding style.