Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use OR condition in a JavaScript IF statement?

People also ask

Can I use && in if statement in JavaScript?

In the logical AND ( && ) operator, if both conditions are true , then the if block will be executed. If one or both of the conditions are false , then the else block will be executed.

Can you use or in an if statement JavaScript?

JavaScript contains conditional statements like if-else , switch cases , etc. These statements are conditional and used to check whether the given condition is true or not; for this, we use OR || and && operators.

Can you put two conditions in an if statement JavaScript?

You can use the logical AND (&&) and logical OR (||) operators to specify multiple conditions in an if statement. When using logical AND (&&), all conditions have to be met for the if block to run.

Can IF statement have 3 conditions JavaScript?

Using either “&&” or “||” i.e. logical AND or logical OR operator or combination of can achieve 3 conditions in if statement JavaScript.


Simply use the logical "OR" operator, that is ||.

if (A || B)

Worth noting that || will also return true if BOTH A and B are true.

In JavaScript, if you're looking for A or B, but not both, you'll need to do something similar to:

if( (A && !B) || (B && !A) ) { ... }

Use the || operator.


if (A || B) { do something }

|| is the or operator.

if(A || B){ do something }

here is my example:

if(userAnswer==="Yes"||"yes"||"YeS"){
 console.log("Too Bad!");   
}

This says that if the answer is Yes yes or YeS than the same thing will happen


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!