Browser and Node Support
You'll learn how to verify compatibility for the nullish coalescing operator (??) and handle environments that don't support it yet.
Understanding Compatibility Requirements
The nullish coalescing operator (??) was introduced in ES2020 (ECMAScript 2020). While modern browsers and recent Node.js versions support it natively, older environments may not recognize the syntax, causing your code to fail with syntax errors.
Browser Support:
- Chrome 80+ (February 2020)
- Firefox 72+ (January 2020)
- Safari 13.1+ (March 2020)
- Edge 80+ (February 2020)
Node.js Support:
- Node.js 14.0.0+ (April 2020)
Checking Your Target Environments
Before using ?? in production, check your audience's browser usage with tools like caniuse.com. If you need to support older browsers (like IE11 or older mobile browsers), you'll need a polyfill strategy.
Polyfill Strategies
Unlike runtime features (like Array.includes()), ?? is syntax-level. This means you can't simply add a function at runtime—the code must be transformed before older engines see it.
Solution: Transpilation
Use a build tool like Babel to convert modern syntax into older equivalents:
// Your modern code
const value = userInput ?? 'default';
// Babel transforms it to something like
const value = userInput !== null && userInput !== undefined ? userInput : 'default';
Configure Babel with @babel/plugin-proposal-nullish-coalescing-operator or use @babel/preset-env with your target environments specified.
Key Takeaway: The nullish coalescing operator requires ES2020 support (Chrome/Firefox/Safari from early 2020, Node 14+); for older environments, use Babel to transpile the syntax into compatible code before deployment.