JS: Nullish Coalescing Operator

By Xah Lee. Date: .

New in JS2020.

x ?? y
Return x if x is not null nor undefined , else return y. This is usually used for assignement, for example, const color = obj.color ?? "red";
console.log((undefined ?? 4) === 4);
console.log((null ?? 4) === 4);

console.log((0 ?? 4) === 0);
console.log((1 ?? 4) === 1);
console.log(("" ?? 4) === "");
console.log(("x" ?? 4) === "x");

// all true

JavaScript, Operators

BUY Ξ£JS JavaScript in Depth