# Logical OR (||)

## 在此页

The logical OR ( ``` || ``` ) operator (logical disjunction) 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.

## 句法

```expr1 || expr2
```

## 描述

``` expr 1 ``` can be converted to ``` true ``` , returns ``` expr 1 ``` ; else, returns ``` expr 2 ``` .

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:

• ``` null ``` ;
• ``` NaN ``` ;
• ``` 0 ``` ;
• empty string ( ``` "" ``` or ``` '' ``` or ``` `` ``` );
• ``` undefined ``` .

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 ``` Boolean ``` 构造函数。

### Short-circuit evaluation

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 ``` is short-circuit evaluated to the truthy expression.

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:

```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)
```

### 运算符优先级

The following expressions might seem equivalent, but they are not, because the ``` && ``` operator is executed before the ``` || ``` operator (see 运算符优先级 ).

```true || false && false      // returns true, because && is executed first
(true || false) && false    // returns false, because operator precedence cannot apply
```

## 范例

### Using OR

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

```o1 = true  || true       // t || t returns true
o2 = false || true       // f || t returns true
o3 = true  || false      // t || f returns true
o4 = false || (3 == 4)   // f || f returns false
o5 = 'Cat' || 'Dog'      // t || t returns "Cat"
o6 = false || 'Cat'      // f || t returns "Cat"
o7 = 'Cat' || false      // t || f returns "Cat"
o8 = ''    || false      // f || f returns false
o9 = false || ''         // f || f returns ""
o10 = false || varObject // f || object returns varObject
```

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

The following composite operation involving booleans :

```bCondition1 && (bCondition2 || bCondition3)
```

is always equal to:

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

## 浏览器兼容性

 Desktop Mobile Server Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Chrome 1 Edge 12 Firefox 1 IE 3 Opera 3 Safari 1 WebView Android 1 Chrome Android 18 Firefox Android 4 Opera Android 10.1 Safari iOS 1 Samsung Internet Android 1.0 nodejs 0.1.100

## 另请参阅

1. JavaScript
2. 教程：
3. 完整初学者
4. JavaScript 指南
5. 中间体
6. 高级
7. 参考：
8. 内置对象
9. 表达式 & 运算符
10. 语句 & 声明
11. 函数
12. 错误
13. 杂项