Short-Circuiting in JavaScript
Tags: javascript, webdev, programming, node
Published: 2021-11-25 14:23:16


In JavaScript, **short-circuiting refers to the partial evaluation of expressions to avoid unnecessary computation**. This blog post describes the short-circuiting operators and assignments in JavaScript and how to use them.

### Short-Circuiting Operators: &&, ||, and ??

Short-circuiting operators only evaluate the right side of an expression when necessary. 

For example, for the logical AND operator `&&`, when the left side of the operator is false, the right side of the operator will not change the result. The result is false regardless of whether the right side is true or false. Therefore JavaScript skips the evaluation of the right side and uses the value from the left side.

There are three short-circuiting binary operators in JavaScript:
* **[Logical AND](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_AND)** (`A && B`): `B` is only evaluated if `A` is [truthy](https://developer.mozilla.org/en-US/docs/Glossary/Truthy).
* **[Logical OR](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_OR)** (`A || B`): `B` is only evaluated if `A` is [falsy](https://developer.mozilla.org/en-US/docsGlossary/Falsy).
* **[Nullish coalescing](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing_operator)** (`A ?? B`): `B` is only evaluated if `A` is [nullish](https://developer.mozilla.org/en-US/docs/Glossary/Nullish).

`A` and `B` can be any expression. Their evaluation might invoke function and getter calls that can contain complex calculations or have side effects.
Because `A` and `B` can return any value, the three short-circuiting operators evaluate and return any value, not just booleans.

This means that you can use the short-circuiting operators for **providing defaults** (`||` and `??`), for checking nullish values (`&&`, before the optional chaining operator `?.` was available), and for **[conditional rendering in React](https://reactjs.org/docs/conditional-rendering.html#inline-if-with-logical--operator)** (`&&`).

```javascript
// default values
a = a || 123; // assigns 123 to a if a is falsy
b = b ?? 123; // assigns 123 to b if b is nullish

// optional chaining with && ( .? is a modern alterative)
if (obj.m != null && obj.m() === '123') {
   // ...
}

// React: conditional rendering
return <>  
  {user != null &&
    <div>Welcome back, ${user.name}!</div>
  }
<>;
```

### Short-Circuiting Assignments: &&=, ||=, and ??=

**With [ES2021](https://h3manth.com/ES2021/), you can use short-circuiting operators in assignment expressions** (`&&=`, `||=`, and `??=`). Short-circuiting assignments are only carried out when the current variable or property value does not trigger short-circuiting. This behavior can help avoid unnecessary updates.

Here is an example(`-Infinity` is [truthy](https://developer.mozilla.org/en-US/docs/Glossary/Truthy)):
```javascript
let a = 3;
let b = 3;
let c = 0;
let d = 0;

a &&= -Infinity;
b ||= -Infinity;
c &&= -Infinity;
d ||= -Infinity;

console.log(a); // -Infinity
console.log(b); // 3 (short-circuiting ||, because 3 is truthy)
console.log(c); // 0 (short-circuiting &&, because 0 is falsy)
console.log(d); // -Infinity
```

### Refactoring Assignments with Short-Circuiting Operators

Short-circuiting assignments look very similar to regular assignments with short-circuiting operator expressions. One might think that they can be refactored as follows without changing the behavior:
```javascript
a = a && x; /* becomes */ a &&= x;
a = a || x; /* becomes */ a ||= x;
a = a ?? x; /* becomes */ a ??= x;
```

However, when I was developing the safety evaluation for the 
'[Push Operator into Assignment](https://p42.ai/documentation/code-action/push-operator-into-assignment)' and '[Pull Operator Out of Assignment](https://p42.ai/documentation/code-action/pull-operator-out-of-assignment)' refactorings in [P42](https://p42.ai/), I discovered that those refactorings can lead to behavioral changes in some situations.

Consider the following example:
```javascript
class C {
  constructor(name) {
    this.name = name;
  }
  
  get x() {
    return this._x;
  }
  
  set x(value) {
    console.log(`set ${this.name}.x to ${value}`);
    this._x = value;
  }
}
```

```javascript
// nullish coalescing operator
const a = new C("a");
a.x = a.x ?? 3;
a.x = a.x ?? 4;
console.log(a.x)
```

```javascript
// nullish assignment 
const b = new C("b");
b.x ??= 3;
b.x ??= 4;
console.log(b.x)
```

Surprisingly, moving the operator into the assignment changes what operations are executed:

```javascript
// output for nullish coalescing operator
"set a.x to 3"
"set a.x to 3"
3
```
```javascript
// output for nullish assignment 
"set b.x to 3"
3
```

While these minor differences won't matter most of the time, it's good to be aware of them for two reasons:
* they can cause breakages during refactoring because they change existing behavior
* they can be beneficial to reduce the number of operations in performance hotspots

### Summary

Short-circuiting operators in JavaScript (`&&`, `||`, `??`) evaluate their right-hand side expression only when necessary. Their assignment equivalents (`&&=`, `||=`, `??=`) only update a value when the current value would 
cause the execution of the right side of the short-circuiting operator.