Does JavaScript Have a Nullish Coalescing Operator: A Practical Guide
Explore how the nullish coalescing operator works in JavaScript, how ?? differs from ||, and practical usage patterns with clear examples for safer, more readable code.

The nullish coalescing operator ?? is a JavaScript operator that returns the right-hand value when the left-hand value is null or undefined.
What is the nullish coalescing operator
According to JavaScripting, the nullish coalescing operator ?? is a concise defaulting mechanism in JavaScript. It returns the right-hand side when the left-hand side is null or undefined, and otherwise returns the left-hand side. This behavior makes it ideal for dealing with optional data from forms, API responses, or nested objects without accidentally replacing valid falsey values like 0 or an empty string. For those wondering does javascript have null coalescing operator, the answer is yes: JavaScript exposes ?? as the nullish coalescing operator since ES2020.
How the ?? operator works in practice
The operator follows a straightforward rule: left ?? right yields left if left is not null or undefined; otherwise, it yields right. Example: const name = user?.name ?? 'Guest'; If user or user.name is null or undefined, name becomes 'Guest'. Importantly, ?? only checks for nullish values. Values like 0, '', or false are considered valid, so they won’t trigger the default. This nuance helps keep meaningful data intact while still providing safe defaults.
?? vs ||: understanding the difference
The logical OR operator || returns the right-hand side when the left-hand side is any falsy value, including 0, '', false, NaN, null, or undefined. In contrast, ?? reacts only to nullish values. For example, 0 ?? 5 yields 0, whereas 0 || 5 yields 5. This distinction matters when zero or empty strings are legitimate values in your data model and should not be replaced by defaults.
Syntax and common patterns
The basic syntax is const result = a ?? b; You can chain ?? to provide multiple fallbacks, as in const mode = env?.mode ?? 'default'; If you have deeply nested structures, combining nullish checks with optional chaining keeps code short and readable. Always position defaults clearly to avoid misinterpretation during code reviews.
Practical examples in code
Using ?? with optional chaining lets you guard against deep data structures without verbose checks. For instance:
function getDiscount(user) {
return user?.preferences?.discount ?? 0;
}
Another common pattern is assigning safe defaults for configuration objects:
const timeout = options?.timeout ?? 5000;
These patterns prevent runtime errors when properties are missing or undefined.
Compatibility and transpilation
Most modern runtimes support ?? since ES2020. If you work with older browsers or Node.js versions, turn to transpilation via Babel or TypeScript to bridge the gap. Be mindful of the interaction between ?? and ?:. Precedence can surprise when combined with other operators, so use parentheses when needed, for example: const value = (a ?? b) ?? c;
Pitfalls to watch and how to test
Common pitfalls include assuming ?? behaves like || in all cases and overlooking precedence rules. Always test with values like 0, '', false, null, and undefined to confirm your logic. Automated tests and linting rules can catch precedence or parentheses issues before code reaches production.
Related patterns and alternatives you should know
Beyond ?? you should know optional chaining (?.) for safe property access and default parameters in function signatures. In some cases a small helper function to normalize inputs can provide clearer intent than repeated ?? usage across a module. Pair ?? with ?. to build robust, readable data access layers.
Questions & Answers
What does the nullish coalescing operator do in JavaScript?
The ?? operator returns the right-hand side when the left-hand side is null or undefined; otherwise it returns the left-hand side. It provides a safe default mechanism for optional values.
The ?? operator returns the right value if the left is null or undefined, otherwise it returns the left value.
Is the nullish coalescing operator the same as the logical OR operator?
No. ?? only triggers on null or undefined, while || triggers on any falsy value like 0, '', false, or NaN. This distinction affects when defaults are applied.
No. ?? handles only null and undefined. Or handles any falsy value.
Can I use ?? with optional chaining or other operators?
Yes. You can combine ?? with optional chaining to provide defaults after safely accessing nested properties. Use parentheses to avoid precedence surprises when mixing with other operators.
Yes you can use ?? with optional chaining to provide defaults after safe access, usually with parentheses when mixed with other operators.
Which environments support the nullish coalescing operator?
Support exists in ES2020 compatible environments. If you need older environments, use a transpiler or polyfill to backfill the feature.
Most modern environments support it; older ones may require transpilation.
What are common pitfalls when using ??
Assuming ?? behaves like || in all cases and overlooking precedence rules. Always test with various values like 0, '', and undefined to verify defaults are applied correctly.
Common pitfalls include confusing ?? with || and neglecting operator precedence.
How should I test code that uses ??
Write unit tests that cover null, undefined, and valid values like 0 or '' to confirm defaults kick in only when appropriate. Include integration tests for data flowing from APIs.
Test with null and undefined to check defaults, and with 0 or '' to ensure values pass through.
What to Remember
- Use ?? to provide defaults for nullish values only.
- Differentiate ?? from || to avoid overwriting legitimate values.
- Combine ?? with optional chaining for safe deep access.
- Be mindful of precedence and use parentheses when mixing with other operators.
- Test behavior with null and undefined across your supported environments.