> For the complete documentation index, see [llms.txt](https://edrus.gitbook.io/mt-it/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://edrus.gitbook.io/mt-it/2nd-month/week-5/javascript/expressions-and-operators/logical-operators.md).

# Logical operators

Logical operators are typically used with Boolean (logical) values; when they are, they return a Boolean value. However, the `&&` and `||` operators actually return the value of one of the specified operands, so if these operators are used with non-Boolean values, they may return a non-Boolean value. The logical operators are described in the following table.

| Operator                                                                                                      | Usage              | Description                                                                                                                                                                                             |
| ------------------------------------------------------------------------------------------------------------- | ------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [Logical AND](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_AND) (`&&`) | `expr1 && expr2`   | Returns `expr1` if it can be converted to `false`; otherwise, returns `expr2`. Thus, when used with Boolean values, `&&` returns `true` if both operands are true; otherwise, returns `false`.          |
| [Logical OR ](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_OR)(`\|\|`) | `expr1 \|\| expr2` | Returns `expr1` if it can be converted to `true`; otherwise, returns `expr2`. Thus, when used with Boolean values, `\|\|` returns `true` if either operand is true; if both are false, returns `false`. |
| [Logical NOT](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_NOT) (`!`)  | `!expr`            | Returns `false` if its single operand that can be converted to `true`; otherwise, returns `true`.                                                                                                       |

Examples of expressions that can be converted to `false` are those that evaluate to null, 0, NaN, the empty string (""), or undefined.

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

JSCopy to Clipboard

```javascript
const a1 = true && true; // t && t returns true
const a2 = true && false; // t && f returns false
const a3 = false && true; // f && t returns false
const a4 = false && 3 === 4; // f && f returns false
const a5 = "Cat" && "Dog"; // t && t returns Dog
const a6 = false && "Cat"; // f && t returns false
const a7 = "Cat" && false; // t && f returns false
```

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

JSCopy to Clipboard

```javascript
const o1 = true || true; // t || t returns true
const o2 = false || true; // f || t returns true
const o3 = true || false; // t || f returns true
const o4 = false || 3 === 4; // f || f returns false
const o5 = "Cat" || "Dog"; // t || t returns Cat
const o6 = false || "Cat"; // f || t returns Cat
const o7 = "Cat" || false; // t || f returns Cat
```

The following code shows examples of the ! (logical NOT) operator.

JSCopy to Clipboard

```javascript
const n1 = !true; // !t returns false
const n2 = !false; // !f returns true
const n3 = !"Cat"; // !t returns false
```

#### [Short-circuit evaluation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators#short-circuit_evaluation) <a href="#short-circuit_evaluation" id="short-circuit_evaluation"></a>

As logical expressions are evaluated left to right, they are tested for possible "short-circuit" evaluation using the following rules:

* `false && anything` is short-circuit evaluated to false.
* `true || anything` is short-circuit evaluated to true.

The rules of logic guarantee that these evaluations are always correct. Note that the *anything* part of the above expressions is not evaluated, so any side effects of doing so do not take effect.

Note that for the second case, in modern code you can use the [Nullish coalescing operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing) (`??`) that works like `||`, but it only returns the second expression, when the first one is "[nullish](https://developer.mozilla.org/en-US/docs/Glossary/Nullish)", i.e. [`null`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/null) or [`undefined`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined). It is thus the better alternative to provide defaults, when values like `''` or `0` are valid values for the first expression, too.
