Nullish coalescing (??)
Returns the right-hand value if the left is null or undefined.
Available since: ES2020
Syntax
value ?? defaultValueExamples
A default value:
const name = null;
console.log(name ?? 'Guest');Output:
GuestThe difference from || — 0 and empty strings are kept:
const count = 0;
console.log(count ?? 10);
console.log(count || 10);Output:
0
10Notes
Use ?? for defaults when 0, '' and false are valid values. The || operator replaces any falsy value.
See also
- Logical OR (||) — Logical OR: returns the first truthy value.
- Optional chaining (?.) — Safe property access: returns undefined instead of an error if the object is missing.
- Logical assignment (??=, ||=, &&=) — Assigns a value only if the variable is nullish, falsy or truthy.
Related recipes
In the tutorial
- Logical operators — JavaScript logical operators — AND, OR, NOT, their precedence, default values with || and the ?? operator.