逻辑或 (||)

当且仅当其一个或多个操作数为 true 时,一组操作数的逻辑 OR (||)(逻辑或)运算符才为 true。它通常与布尔(逻辑)值一起使用。如果是,则返回一个布尔值。但是,|| 运算符实际上返回指定操作数之一的值,因此如果该运算符与非布尔值一起使用,它将返回非布尔值。

¥The logical OR (||) (logical disjunction) operator for a set of operands is true if and only if one or more of its operands is true. It is typically used with boolean (logical) values. When it is, it returns a Boolean value. However, the || operator actually returns the value of one of the specified operands, so if this operator is used with non-Boolean values, it will return a non-Boolean value.

Try it

语法

¥Syntax

js
x || y

描述

¥Description

如果 x 可以转换为 true,则返回 x;否则,返回 y

¥If x can be converted to true, returns x; else, returns y.

如果一个值可以转换为 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:

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

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

¥Even though the || operator can be used with operands that are not Boolean values, it can still be 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

逻辑 OR 表达式从左到右计算,并使用以下规则测试可能的 "short-circuit" 计算:

¥The logical OR expression is evaluated left to right, it is tested for possible "short-circuit" evaluation using the following rule:

(some truthy expression) || expr 被短路评估为真值表达式。

¥(some truthy expression) || expr is short-circuit evaluated to the truthy expression.

短路意味着上面的 expr 部分没有被评估,因此这样做的任何副作用都不会生效(例如,如果 expr 是函数调用,则调用永远不会发生)。发生这种情况是因为在评估第一个操作数之后运算符的值已经确定。参见示例:

¥Short circuit means that the expr part above is not evaluated, hence any side effects of doing so do not take effect (e.g., if expr is a function call, the calling never takes place). This happens because the value of the operator is already determined after the evaluation of the first operand. See example:

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

console.log(B() || A());
// Logs "called B" due to the function call,
// then logs true (which is the resulting value of the operator)

运算符优先级

¥Operator precedence

以下表达式可能看起来等效,但实际上并非如此,因为 && 运算符在 || 运算符之前执行(请参阅 运算符优先级)。

¥The following expressions might seem equivalent, but they are not, because the && operator is executed before the || operator (see operator precedence).

js
true || false && false; // returns true, because && is executed first
(true || false) && false; // returns false, because grouping has the highest precedence

示例

¥Examples

使用或

¥Using OR

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

¥The following code shows examples of the || (logical OR) operator.

js
true || true; // t || t returns true
false || true; // f || t returns true
true || false; // t || f returns true
false || 3 === 4; // f || f returns false
"Cat" || "Dog"; // t || t returns "Cat"
false || "Cat"; // f || t returns "Cat"
"Cat" || false; // t || f returns "Cat"
"" || false; // f || f returns false
false || ""; // f || f returns ""
false || varObject; // f || object returns varObject

注意:如果你使用此运算符为某个变量提供默认值,请注意不会使用任何虚假值。如果只需要过滤掉 nullundefined,可以考虑使用 空值合并运算符

¥Note: If you use this operator to provide a default value to some variable, be aware that any falsy value will not be used. If you only need to filter out null or undefined, consider using the nullish coalescing operator.

布尔值的转换规则

¥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 following some rules.

以下涉及布尔值的复合运算:

¥The following composite operation involving booleans:

js
bCondition1 && (bCondition2 || bCondition3)

总是等于:

¥is always equal to:

js
!(!bCondition1 || !bCondition2 && !bCondition3)

规范

Specification
ECMAScript Language Specification
# prod-LogicalORExpression

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看