nullish coalescing (??)
In JavaScript, the nullish coalescing operator (??) returns its right-hand operand when its left-hand operand is null or undefined, and returns the left-hand operand otherwise. The right-hand side is evaluated only when the left-hand side is nullish, which is a form of short-circuit evaluation.
Only null and undefined count as missing values. Other falsy values, such as 0, "", false, and NaN, pass through unchanged. That makes ?? the operator for defaults where those values are valid data, such as a volume of 0 or an empty search query.
JavaScript Nullish Coalescing Syntax
The nullish coalescing operator sits between a value that might be missing and its fallback:
<leftExpression> ?? <fallbackExpression>
A chain such as a ?? b ?? c groups from the left and returns the first operand that isn’t nullish, or c when all the others are.
JavaScript Nullish Coalescing Examples
Say a video player reads saved settings, where a volume of 0 and an empty caption language are deliberate choices:
> const settings = { volume: 0, captionLanguage: "", theme: null };
undefined
> settings.volume ?? 50
0
> settings.captionLanguage ?? "en"
''
> settings.theme ?? "dark"
'dark'
> settings.speed ?? 1
1
The 0 and the empty string survive because they aren’t nullish. The null theme and the missing speed property, which reads as undefined, both fall back to their defaults.
The same lookup with logical OR (||) replaces the muted volume, because || falls back on every falsy value:
> const volume = 0;
undefined
> volume || 50
50
> volume ?? 50
0
Mixing ?? with || or && needs parentheses. Without them, the parser rejects the line before it runs:
> const nickname = "";
undefined
> const fullName = null;
undefined
> nickname || fullName ?? "Guest"
nickname || fullName ?? "Guest"
^^
Uncaught SyntaxError: Unexpected token '??'
> (nickname || fullName) ?? "Guest"
'Guest'
The parentheses state which operator applies first, so the expression parses and returns the fallback.
JavaScript Nullish Coalescing Gotchas
- An expression that mixes
??with||or&&without parentheses is aSyntaxError, even though each operator has a defined precedence. - The right-hand side runs only on a fallback, so a function call placed there isn’t evaluated when the left-hand side has a value.
- A property explicitly set to
undefinedgets the default, just like a property that doesn’t exist.
JavaScript Nullish Coalescing Use Cases
- Supplying a default value when a setting is
nullorundefined - Keeping valid falsy values such as
0,"", andfalsefrom configuration files or form input - Providing a fallback at the end of an optional chain, as in
user.address?.city ?? "Unknown" - Defaulting a function argument that callers may pass as
null, which default parameters don’t replace
Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.
By Leodanis Pozo Ramos • Updated Nov. 12, 2026