Nullish Coalesning Operator

[!INFO]-
topic: 🖥️ Tech
links: Handling optional properties in TypeScript with the Elvis operator
source: Nullish coalescing operator (??) - JavaScript | MDN
tags: #permanent-note #published

Last Modified: =dateformat(this.file.mtime, "MMM. dd, yyyy - HH:mm")


?? is like || but it only reacts to undefined and null.

The nullish coalescing ?? operator is a logical operator that returns its right-hand side operand when its left-hand side operand is null or undefined, and otherwise returns its left-hand side operand.

const foo = null ?? 'default string';
console.log(foo);
// Expected output: "default string"

const baz = 0 ?? 42;
console.log(baz);
// Expected output: 0

It is a more detailed version of || which reacts to falsy values, while ?? only reacts to null and undefined.