Nullish Coalescing Operator
Version: ES2020
Level: Intermediate
The nullish coalescing operator (??
) is also new to ES2020. It works much like logical OR (||
) except that it will short circuit (or return) for non-null, non-undefined falsy values such as false, 0, and empty strings whereas OR would not. This is useful for setting defaults that may include these kinds of values.
let initialValue = 0;
let x = initialValue || 1;
let y = initialValue ?? 1;
console.log(x); // 1
console.log(y); // 0
More info: