Most developers are not aware of the behavior of AND or OR operator

Yashraj basan
2 min readJan 24, 2020

You might be thinking “what’s new in the && (and) and ||(or) operators. They are just logical operators” but there is something about these two operator that most developers don’t know. They know how to use, but they do not know how the && and || operator works internally.

How && operator works internally

The AND operator is a logical operator, which returns true when both conditions are true. Otherwise, it returns false.

Consider this simple pseudo-code -

Imagine this is a real-world life example. If condition1 determined false then && operator will not be going to determine condition2. it simply returns false and “else” block will be executed. There is no need to determine condition2 because && operator returns true if and only if condition1 and condition2 are true and it is also time-wasting because && operator found false. If condition1 is true then && operator will be going to determine condition2.

How || operator works internally

The OR operator is a logical operator, which returns true if one condition is true. Otherwise, it returns false.

Consider this simple pseudo-code -

Now whatever behavior you have seen in && operator same behavior you will find in || operator but here if condition1 is true then it will simply return true and “if” block will be executed. Because || operator returns true if and if one condition is true. If condition1 is false then || operator determines the condition2.

Conclusion

If the first condition is false then && operator will not determine the second condition and it will return directly false. If the first condition is true then || operator will not determine the second condition and it will returns directly true.

I hope you will find this article helpful.

--

--