Sub topic 13
Browser, Node.js and Transpiler Support
This chapter assumes you know the operators ?., ?? and ??= from earlier chapters. It also assumes you have a basic idea of what a build tool or transpiler does. A transpiler is a tool that rewrites modern JavaScript into older syntax. The question here is practical: where can you use these operators, and what happens when the target environment is too old?
Which ECMAScript Version Added What
The three operators did not arrive at the same time.
| Operator | Name | ECMAScript version |
|---|---|---|
| `?.` | Optional chaining | ES2020 |
| `??` | Nullish coalescing | ES2020 |
| `??=` | Nullish coalescing assignment | ES2021 |
Runtime Support
Every current evergreen browser supports all three operators. An evergreen browser is one that updates itself automatically. Older engines that are still in use are a different story.
| Runtime | `?.` and `??` | `??=` |
|---|---|---|
| Chrome / Edge | 80+ | 85+ |
| Firefox | 74+ (`??` from 72) | 79+ |
| Safari | 13.1+ | 14+ |
| Node.js | 14+ | 15+ |
| Internet Explorer | Never | Never |
The old engine does not fail at runtime. It fails at parse time. An engine without support sees ?. as invalid syntax and throws a SyntaxError before any code in that file runs. A single ?. in a bundle can therefore break a whole page on an old browser.
function supportsSyntax(source) {
try {
new Function(source);
return true;
} catch (err) {
return false;
}
}
console.log("?. :", supportsSyntax("const o = null; o?.x;"));
console.log("?? :", supportsSyntax("const v = null ?? 1;"));
console.log("??= :", supportsSyntax("let a = null; a ??= 1;"));On Node.js 14 this prints true, true, false. On Node.js 15 or later all three print true. Pages with a strict Content Security Policy may block new Function, so most projects rely on build-time transpiling instead.
Transpiling for Older Targets
Babel and TypeScript can both rewrite these operators into equivalent ES5-compatible code.
- Babel:
@babel/preset-envincludes the transforms since version 7.8. If yourtargets(or browserslist config) list an engine without support, the preset rewrites the operators for you. - TypeScript: Version 3.7 added
?.and??, and 4.0 added??=. WhentargetisES2019or lower, the compiler rewrites them.
The output follows a fixed pattern. Take this input:
const name = user?.profile?.name ?? "Anonymous";The compiled result looks roughly like this:
var _a, _b;
const name =
(_b = (_a = user) === null || _a === void 0 ? void 0 : _a.profile) === null ||
_b === void 0
? void 0
: _b.name;
const result = name !== null && name !== void 0 ? name : "Anonymous";The temporary variables matter. They make sure each part of the chain is evaluated only once, so getters and function calls do not run twice. The compiled code also checks for null and undefined explicitly. It never falls back to a truthiness check, so 0 and "" behave the same as in native code.
Common Build Errors
- Webpack 4 parse failures. Webpack 4 uses an older version of the Acorn parser that cannot read
?.. You get an error such asModule parse failed: Unexpected token. Either upgrade to Webpack 5 or transpile the files with Babel before Webpack parses them. - Untranspiled
node_modules. Many build setups skipnode_modules. If a dependency ships modern syntax, old browsers still break. Add that package to your transpile include list. - Mismatched targets. If TypeScript targets
ES2020but you deploy to Node.js 12, the operators are left as they are and the code crashes on load. Matchtargetto the oldest runtime you actually run.
Exercise
Your app runs on Node.js 14. A teammate writes config.port ??= 8080; and the service fails to start with a SyntaxError. Why, and what are two ways to fix it?
Show answer
??= arrived in ES2021 and Node.js supports it from version 15. Node.js 14 supports ?. and ?? but not ??=, so the file fails to parse. One fix is to upgrade to Node.js 15 or later. The other is to transpile with TypeScript (target ES2020 or lower) or Babel, or to write config.port = config.port ?? 8080; by hand.
Exercise
A Babel-compiled bundle works in Chrome but shows a blank page in an old browser, and the error points to ?. inside a file under node_modules. What is the likely cause?
Show answer
The build skips node_modules when transpiling, so the dependency's optional chaining reaches the old browser unchanged and fails to parse. Add that package to the files Babel transpiles.