The JavaScript Comparison Operators are used to compare two variables, what if we want to compare more than one condition? Very simple, JavaScript Logical Operators will do the trick for you.
The JavaScript Logical operators 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
OPERATORS | NAME | DESCRIPTION | EXAMPLE |
---|---|---|---|
&& | AND | It will return True when both conditions are true | If (age > 18 && age <=35) |
|| | OR | It will returns True when at-least one of the condition is true | If (age > 35 || age < 60) |
! | NOT | If the condition is True, logical NOT operator makes it false | If age = 18 then, !( age = 18) returns false. |
Let us see the truth tables behind the JavaScript Logical Operators for better understanding
&& (JavaScript LOGICAL AND)
Condition 1 | Condition 2 | Condition 1 && Condition 2 |
---|---|---|
True | True | True |
True | False | False |
False | True | False |
False | False | False |
|| (JavaScript LOGICAL OR)
Condition 1 | Condition 2 | Condition 1 || Condition 2 |
---|---|---|
True | True | True |
True | False | True |
False | True | True |
False | False | False |
Please refer to JavaScript Comparison Operators article.