Commits


P42 authored and Dominik Moritz committed e6e437a14e9
ARROW-12799: [JS] Use Nullish Coalescing Operator (??) For Defaults **The nullish coalescing operator (`??`) returns its right side when its left side is nullish** (`null` or `undefined`), and its left side otherwise. For example, `const x = a ?? b` would set `x` to `a` if `a` has a value, and to `b` if `a` is `null` or `undefined`. The nullish coalescing operator is very useful to **provide default values when a value or an expression is nullish**. Before its introduction in ES2020, this default value pattern was often expressed using the conditional operator. This refactoring simplifies conditional (ternary) checks to nullish coalescing operator expressions: * `a == null ? x : a` becomes `a ?? x` * `a != null ? a : x` becomes `a ?? x` * `a === null || a === undefined ? x : a` becomes `a ?? x` * `a !== null && a !== undefined ? a : x` becomes `a ?? x` * `f(1) != null ? f(1) : x` becomes `f(1) ?? x` * etc. Learn More: [Nullish coalescing operator (MDN)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing_operator) When two similar-looking function calls have a side effect, this refactoring can change the behavior of the code. For example, the refactoring changes: ```javascript let a = f(1) === null || f(1) === undefined ? 'default' : f(1); ``` into ```javascript let a = f(1) ?? 'default'; ``` If `f(1)` has a side effect, it would have been called one, two or three times before the refactoring, and once after the refactoring. This means that the side effect would have been called a different number of times, potentially changing the behavior. Closes #10339 from domoritz/p42/nullish_coalescing_operator/1621098556103 Authored-by: P42 <72252241+p42-ai[bot]@users.noreply.github.com> Signed-off-by: Dominik Moritz <domoritz@gmail.com>