逻辑与 (&&)

当且仅当所有操作数都是 true 时,一组布尔操作数的逻辑 AND (&&)(逻辑合)运算符才为 true。否则就是 false

¥The logical AND (&&) (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.

更一般地,该运算符返回从左到右计算时遇到的第一个 falsy 操作数的值,或者如果它们都是 truthy,则返回最后一个操作数的值。

¥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.

Try it

语法

¥Syntax

js
x && y

描述

¥Description

逻辑 AND (&&) 从左到右计算操作数,立即返回遇到的第一个 falsy 操作数的值;如果所有值都是 truthy,则返回最后一个操作数的值。

¥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.

如果一个值可以转换为 true,则该值就是所谓的 truthy。如果一个值可以转换为 false,则该值就是所谓的 falsy

¥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.

可以转换为 false 的表达式示例有:

¥Examples of expressions that can be converted to false are:

  • false
  • null
  • NaN
  • 0
  • 空字符串(""'' 或 ````);
  • undefined

AND 运算符保留非布尔值并按原样返回它们:

¥The AND operator preserves non-Boolean values and returns them as they are:

js
result = "" && "foo"; // result is assigned "" (empty string)
result = 2 && 0; // result is assigned 0
result = "foo" && 4; // result is assigned 4

尽管 && 运算符可以与非布尔操作数一起使用,但它仍然被视为布尔运算符,因为它的返回值始终可以转换为 布尔原语。要将其返回值(或一般的任何表达式)显式转换为相应的布尔值,请使用 double NOT operatorBoolean 构造函数。

¥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

逻辑 AND 表达式是短路运算符。由于每个操作数都转换为布尔值,如果发现一次转换的结果为 false,则 AND 运算符停止并返回该假操作数的原始值;它不会评估任何剩余的操作数。

¥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

expr 部分永远不会被计算,因为第一个操作数 (some falsy expression) 被计算为 falsy。如果 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:

js
function A() {
  console.log("called A");
  return false;
}
function B() {
  console.log("called B");
  return true;
}

console.log(A() && B());
// Logs "called A" to the console due to the call for function A,
// && evaluates to false (function A returns false), then false is logged to the console;
// the AND operator short-circuits here and ignores function B

运算符优先级

¥Operator precedence

AND 运算符的优先级高于 OR 运算符,这意味着 && 运算符先于 || 运算符执行(请参阅 运算符优先级)。

¥The AND operator has a higher precedence than the OR operator, meaning the && operator is executed before the || operator (see operator precedence).

js
true || false && false; // true
true && (false || false); // false
(2 === 3) || (4 < 0) && (1 === 1); // false

示例

¥Examples

使用“与”

¥Using AND

以下代码显示了 &&(逻辑 AND)运算符的示例。

¥The following code shows examples of the && (logical AND) operator.

js
a1 = true && true; // t && t returns true
a2 = true && false; // t && f returns false
a3 = false && true; // f && t returns false
a4 = false && 3 === 4; // f && f returns false
a5 = "Cat" && "Dog"; // t && t returns "Dog"
a6 = false && "Cat"; // f && t returns false
a7 = "Cat" && false; // t && f returns false
a8 = "" && false; // f && f returns ""
a9 = false && ""; // f && f returns false

布尔值的转换规则

¥Conversion rules for booleans

将 AND 转换为 OR

¥Converting AND to OR

以下涉及布尔值的运算:

¥The following operation involving booleans:

js
bCondition1 && bCondition2

总是等于:

¥is always equal to:

js
!(!bCondition1 || !bCondition2)

将 OR 转换为 AND

¥Converting OR to AND

以下涉及布尔值的运算:

¥The following operation involving booleans:

js
bCondition1 || bCondition2

总是等于:

¥is always equal to:

js
!(!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:

js
bCondition1 || (bCondition2 && bCondition3)

总是等于:

¥is always equal to:

js
bCondition1 || bCondition2 && bCondition3

规范

Specification
ECMAScript Language Specification
# prod-LogicalANDExpression

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看

¥See also