From 5b47877c27ac57890ba0f20d3004783a91b63f23 Mon Sep 17 00:00:00 2001 From: P42 <72252241+p42-ai[bot]@users.noreply.github.com> Date: Sat, 15 May 2021 17:09:24 +0000 Subject: [PATCH] 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. --- js/bin/integration.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/bin/integration.js b/js/bin/integration.js index c6f6cd7a24ed5..194668e85d4ad 100755 --- a/js/bin/integration.js +++ b/js/bin/integration.js @@ -189,7 +189,7 @@ function compareTableIsh(actual, expected) { function compareVectors(actual, expected) { if ((actual == null && expected != null) || (expected == null && actual != null)) { - throw new Error(`${actual == null ? `actual` : `expected`} is null, was expecting ${actual == null ? expected : actual} to be that also`); + throw new Error(`${actual == null ? `actual` : `expected`} is null, was expecting ${actual ?? expected} to be that also`); } let props = ['type', 'length', 'nullCount'];