Interesting use of && (AND) operator in JavaScript

The && (AND) operator is generally used to check if both sides of the operator are true.

It continues if both sides are true, otherwise, it stops.

Let's see in action:

if(a > 5 && a <10) {console.log("a is between 5 and 10");}
    else {console.log("a is not between 5 and 10"); }

In this example, it continues if a is between 5 and 10, otherwise, it continues with the next block.

This is the general use case of this operator.

I will show you another one.

let a = 10;
a > 5 && console.log("right side of the operator executed.");

What does this exactly mean?

JavaScript code execution is done line by line from left to right of course.

When the AND operator is used if the first part is false, then we do not need to check the second part because it will be false.

But when the first part, the left side of the operator is true we need to go to the right side of the operator.

In our example, the right side is not a boolean. It is executed directly when we pass a first check on the left side.

This is not used to check if both sides are true or at least any of them is false, it is used to execute something when the left side of the operator is true.

If we execute this code it will send right side of the operator executed. to console.

Execution is like this:

- Is the left side true?

-YES.

-Please continue to the right side of the operator.

-It is not checking any boolean.

Do not worry execute it:)

Thanks for reading so far!

Yusuf

twitter.com/yc_crypto