Nullish Coalescing (JavaScript)

The nullish coalescing operator (??) returns its right-hand value only when the left-hand value is null or undefined. It is a safer default than ||.

JavaScript

Unlike ||, which treats 0, "", and false as missing, ?? only falls back when the value is actually null or undefined. This makes it ideal for supplying defaults.

It pairs naturally with optional chaining to provide a fallback for missing data.

Syntax

value ?? fallback

Examples

JavaScript

const count = 0;
console.log(count ?? 10);
console.log(null ?? 10);

Output:

0
10

See also

Back to the Dictionary