JavaScript Logical Operator

The JavaScript Comparison Operators are used to compare two variables, what if we want to compare more than one condition? Very simple, JavaScript Logical Operator will do the trick for you.

The Logical operators in JavaScript Programming language are used to combine two or more conditions and perform the logical operations using && (Logical AND), || (Logical OR) and ! (Logical NOT). The below table describes them

OPERATORSNAMEDESCRIPTIONEXAMPLE
&&Logical ANDIt will return True when both conditions are trueIf (age > 18 && age <=35)
||Logical ORIt will returns True when at-least one of the condition is trueIf (age > 35 || age < 60)
!Logical NOTIf the condition is True, logical NOT operator makes it falseIf age = 18 then, !( age = 18) returns false.

Let us see the truth tables behind the JavaScript Logical Operator for better understanding

&& (JavaScript LOGICAL AND)

Condition 1Condition 2Condition 1 && Condition 2
TrueTrueTrue
TrueFalseFalse
FalseTrueFalse
FalseFalseFalse

|| (JavaScript LOGICAL OR)

Condition 1Condition 2Condition 1 || Condition 2
TrueTrueTrue
TrueFalseTrue
FalseTrueTrue
FalseFalseFalse

Please refer to JavaScript Comparison Operators article.