Logical AND Operator
Logical AND operator takes two operands, and returns true if both left and right operands are true, or false otherwise.
Syntax
&&
symbol is used for logical AND operator.
a && b
Truth table of AND gate
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.