Logical AND Operator
logical AND operator takes two operands, and returns true if both left and right operands are true, or false otherwise
&&
symbol is used for logical AND operator
a && b
the following truth table provides the output of AND operator for possible operand values
a | b | a && b |
---|---|---|
true | true | true |
true | false | false |
false | true | false |
false | false | false |
Examples
1. check if num
is positive and an even number
let num = 12;
if ((num > 0) && (num % 2 == 0)) {
console.log('num is positive and even');
} else {
console.log('num is either not positive or not even');
}
in the above example, left operand a
is (num > 0)
and right operand b
is (num % 2 == 0)
. for given value of num
, both a
and b
are true
. if-condition is true
, and therefore if-block is run