The &&
(logical conjunction) operator for a set of boolean operands will be true
if and only if all the operands are true
. Otherwise it will be false
.
More generally, the operator returns the value of the first falsy operand encountered when evaluating from left to right, or the value of the last operand if they are all truthy.
Description
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.
Short-circuit evaluation
The logical AND expression is a short-circuit operator. As each operand is converted to a boolean, if the result of one conversion is found to be false
, the AND operator stops and returns the original value of that falsy operand; it does not evaluate any of the remaining operands.
Consider the pseudocode below.
(some falsy expression) && expr
The expr
part is never evaluated because the first operand (some falsy expression)
is evaluated as falsy. If expr
is a function, the function is never called. See the example below:
function A() {
console.log("called A");
return false;
}
function B() {
console.log("called B");
return true;
}
console.log(A() && B());
Operator precedence
The AND operator has a higher precedence than the OR operator, meaning the &&
operator is executed before the ||
operator (see operator precedence).
true || false && false;
true && (false || false);
(2 === 3) || (4 < 0) && (1 === 1);
Using AND
The following code shows examples of the &&
(logical AND) operator.
a1 = true && true;
a2 = true && false;
a3 = false && true;
a4 = false && 3 === 4;
a5 = "Cat" && "Dog";
a6 = false && "Cat";
a7 = "Cat" && false;
a8 = "" && false;
a9 = false && "";
Conversion rules for booleans
Converting AND to OR
The following operation involving booleans:
bCondition1 && bCondition2
is always equal to:
!(!bCondition1 || !bCondition2)
Converting OR to AND
The following operation involving booleans:
bCondition1 || bCondition2
is always equal to:
!(!bCondition1 && !bCondition2)
Removing nested parentheses
As logical expressions are evaluated left to right, it is always possible to remove parentheses from a complex expression provided that certain rules are followed.
The following composite operation involving booleans:
bCondition1 || (bCondition2 && bCondition3)
is always equal to:
bCondition1 || bCondition2 && bCondition3
Specifications
Browser compatibility
|
Desktop |
Mobile |
Server |
|
Chrome |
Edge |
Firefox |
Opera |
Safari |
Chrome Android |
Firefox for Android |
Opera Android |
Safari on IOS |
Samsung Internet |
WebView Android |
Deno |
Node.js |
Logical_AND |
1 |
12 |
1 |
3 |
1 |
18 |
4 |
10.1 |
1 |
1.0 |
4.4 |
1.0 |
0.10.0 |