JS: Nullish Coalescing Operator (defined or)

By Xah Lee. Date: . Last updated: .

(new in JS: ECMAScript 2020)

x ?? y

If x is defined, return x, else return y.

defined means it is not null nor undefined

usually used for assignement, e.g. 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");

JavaScript. Operators