Nullish coalescing (??)
Returns the right operand only when the left operand is null or undefined.
Syntax
left ?? right
Returns
any — `left` if it is not `null`/`undefined`, otherwise `right`.
Examples
const a = null;
console.log(a ?? "default");
Output
default
const count = 0;
console.log(count ?? 10);
Output
0
const name = "";
console.log(name ?? "anonymous");
Notes
- Unlike `||`, only `null` and `undefined` are treated as "missing";
`0`, `""`, and `false` pass through.
- Cannot be combined directly with `&&` or `||` without parentheses.
Browser & runtime support
| Environment | Since version |
|---|---|
| chrome | 80 |
| firefox | 72 |
| safari | 13.1 |
| edge | 80 |
| node | 14.0 |