Nullish coalescing
The nullish coalescing operator returns a fallback only when the left side is null or undefined. Unlike ||, it preserves 0, false, and empty strings.
Overview
The nullish coalescing operator returns a fallback only when the left side is null or undefined. Unlike ||, it preserves 0, false, and empty strings.
Browser support
| Feature | Desktop | Mobile | ||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Safari | Chrome Android | Safari iOS | |
| 80 | 80 | 72 | 13.1 | 80 | 13.4 | |
| Other | ||||||
| The nullish coalescing assignment (??=) operator, also known as the logical nullish assignment operator, only evaluates the right operand and assigns to the left if the left operand is nullish (null or undefined). | 85 | 85 | 79 | 14 | 85 | 14 |
Syntax
const value = null ?? 'default'; // 'default'
const zero = 0 ?? 'default'; // 0
const empty = '' ?? 'default'; // ''
// Difference from ||
0 || 'default'; // 'default' (0 is falsy)
0 ?? 'default'; // 0 (0 is not null or undefined) Live demo
Defaults without losing zero
Compare || and ?? to see why numeric zero often needs the nullish version.
Configuration fallback
Apply defaults only to missing options while keeping explicit empty or false values.
Use cases
Applying defaults safely
Keep explicit values such as 0 or an empty label instead of replacing them accidentally.
Reading optional settings
Use ?? when configuration objects may omit a field but should preserve intentionally empty values.
Cautions
- ?? is about missing values only; if falsy values should also fall back, || may still be the right tool.
- Mixing ?? with || or && requires parentheses in JavaScript syntax.
Accessibility
- When you preserve empty strings intentionally, make sure the interface still exposes an accessible label or help text elsewhere.
Related links
Powered by web-features