JavaScript: Logical Operators — AND, OR, NOT
The logical operators used in Javascript are listed below:
Operator | Usage | Description |
---|---|---|
Logical AND ( && ) | a && b | is true if both a and b are true. |
Logical OR ( || ) | a || b | is true if either a or b is true. |
Logical NOT ( ! ) | !a | is true if a is not true. |
JavaScript Logical AND operator (&&)
The following conditions are true :
The following conditions are false :
This above pictorial helps you to understand the concept of LOGICAL AND operation with an analogy of taps and water.
In fig.-1 of the picture, both of the taps are closed, so the water is not flowing down. Which explains that if both of conditions are FALSE or 0, the return is FALSE or 0.
In fig.-2 of the picture, one of the taps are closed, even then, the water is not flowing down. Which explains that even if any of conditions are FALSE or 0, the return is FALSE or 0.
fig.-3 of the picture resembles CASE -2.
In fig.-4 of the picture, both of the taps are open, so the water is flowing down. Which explains that if both of conditions are TRUE or 1, the return is TRUE or 1.
So we can conclude that if and only if, both of the conditions are TRUE or 1, LOGICAL AND operations returns TRUE or 1.
The following web document demonstrates the use of AND operator (&&).
h1 JavaScript equal operator (==) example