Logical AND (&&
) evaluates operands from left to right, returning immediately with the value of the first falsy operand it encounters; if all values are truthy, the value of the last operand is returned.
If a value can be converted to true
, the value is so-called truthy. If a value can be converted to false
, the value is so-called falsy.
Examples of expressions that can be converted to false are:
-
false
;
-
null
;
-
NaN
;
-
0
;
- empty string (
""
or ''
or ``
);
-
undefined
.
The AND operator preserves non-Boolean values and returns them as they are:
result = '' && 'foo';
result = 2 && 0;
result = 'foo' && 4;
Even though the &&
operator can be used with non-Boolean operands, it is still considered a boolean operator since its return value can always be converted to a boolean primitive. To explicitly convert its return value (or any expression in general) to the corresponding boolean value, use a double NOT operator
or the Boolean
constructor.