How To Specify Multiple Conditions In An If Statement In Javascript?

If the conditional statement is no stranger to all of us, it is a statement that is used quite a lot when we use JavaScript, whether the project is large or not. However, only some know how to specify multiple conditions in an if statement in Javascript. So in this article, I will use two logical operators, OR and AND, through a few small examples so we can understand and re-implement.

How did I specify multiple conditions in an if statement in Javascript?

Using the Logical OR (||)

We can use Logical OR (||) to concatenate multiple conditions that are usually boolean values. The result of a sentence using Logical OR (||) will be accurate if one of the conditional statements together with Logical OR (||) is true and false if all of the conditionals are false.

Syntax

expr1 || expr2

Example

const result1 = false || false || true; // true
const result2 = false || false || false; // false
const result3 = true || true || true; // true

When using Logical OR (||), we can combine two or more conditional statements and turn them into a new conditional with a boolean value. We can specify multiple conditions in an if statement in Javascript as follows:

const con1 = true;
const con2 = false;
const con3 = false;

if (con1 || con2 || con3) {
    console.log("result: true");
} else {
    console.log("result: false");
}

if (con3 || con2) {
    console.log("result: true");
} else {
    console.log("result: false");
}

Output

result: true
result: false

Using the Logical AND (&&)

Logical AND (&&) can be used to concatenate conditions in an IF statement, and the results of conditions joined by Logical AND (&&) have a boolean value. The result is true with logical AND (&&) when all the sentence conditions are true. Otherwise, if the result is false, it means that one or more conditions in the sentence are false.

Syntax

expr1 && expr2

Example

const result1 = false && false && true; // false
const result2 = false && false && false; // false
const result3 = true && true && true; // true

We see with Logical AND (&&) the case that the true result is rare because, in the code, if there is only one condition that has a false value, then the whole code will be false. Only if all the conditions in the sentence must be true can we get the result true.

I include the following code that uses Logical AND (&&) to specify multiple conditions:

const con1 = true;
const con2 = false;
const con3 = true;

if (con1 && con2 && con3) {
    console.log("result: true");
} else {
    console.log("result: false");
}

if (con3 && con1) {
    console.log("result: true");
} else {
    console.log("result: false");
}

Output

result: false
result: true

Summary

Above are the two I use to specify multiple conditions in an if statement in Javascript. Depending on the case we choose between the two above, we can combine the two logical OR (||) and logical AND (&&) in some cases. Try it out and give yourself a few more examples. This will help you understand it and use the two operators most flexibly and efficiently. I hope this article helps you and your program good luck.

Leave a Reply

Your email address will not be published. Required fields are marked *