What separates syntax lowering from a polyfill in a web build, and which one fixes `stop?.platform` versus `departures.at(-1)` on an older browser?
answer
- rewrite versus add
- build time versus run time
- SyntaxError against is-not-a-function
- operators rewritten, built-ins supplied
- some features need the engine itself
basics
~20 sLowering rewrites newer syntax, such as optional chaining, into older equivalent code at build time; a polyfill ships runtime code that defines a missing built-in, such as Array.prototype.at. Neither can add an engine capability like Proxy.
solid answer
~40 sSyntax lowering is a build-time rewrite: the transpiler turns `stop?.platform` into a null check an older parser accepts, rewrites class fields into initialisation code in the constructor and turns `async` functions into generator or state-machine code. A polyfill is runtime code: it checks whether `Array.prototype.at`, `structuredClone` or `Promise.withResolvers` exists and defines it if not, so the call site stays as written. The error tells you which one is missing: syntax that escaped lowering fails with a `SyntaxError` before any of that file runs, while a missing built-in throws `TypeError: ... is not a function` or a `ReferenceError` only when that line executes. Some features fit neither: `Proxy` and `BigInt` need the engine itself, so for those you change the code or stop supporting that browser.
code
javascript · 20 lines// polyfills.js, loaded before the app code
if (!Array.prototype.at) {
Object.defineProperty(Array.prototype, 'at', {
value(n) {
n = Math.trunc(n) || 0;
if (n < 0) n += this.length;
return n < 0 || n >= this.length ? undefined : this[n];
},
writable: true,
configurable: true,
});
}
// timetable.js as written
const platform = stop?.platform;
const last = departures.at(-1);
// what a build lowering for an older target could emit for timetable.js:
// const platform = stop == null ? void 0 : stop.platform;
// const last = departures.at(-1); // unchanged: valid syntax, needs the polyfillgo deeper
Recall the split cleanly: lowering rewrites syntax at build time, polyfills define missing built-ins at run time. Give one example of each and say which error each kind of gap produces.
Explain how the error type points at the fix: a parse-time SyntaxError means syntax escaped lowering, a runtime TypeError or ReferenceError means a missing built-in. Mention the helpers and generator runtime lowering adds.
Show you can triage a field crash from the error and the user agent, check whether the failing code came from your source or an untransformed dependency, and name the features no build can rescue, such as Proxy.
Frame it as a support-policy cost: every older browser you keep adds lowering, helpers and polyfills to everyone's bundle unless you split builds, and some features cannot reach that browser at all.
## Two different problems an older browser has When a timetable app written in current JavaScript reaches an older embedded browser, two separate things can be missing: - **Syntax** the parser does not understand: optional chaining (`a?.b`), nullish coalescing (`??`), class fields, `async` functions, the exponentiation operator. - **Built-ins** the runtime does not define: methods such as `Array.prototype.at` or `Array.prototype.findLast`, statics such as `Promise.withResolvers`, and web platform globals such as `structuredClone`. A web build answers these with two different tools. **Syntax lowering** (also called transpiling or down-levelling) rewrites the code at build time. A **polyfill** is a piece of runtime code that supplies a missing built-in. Confusing the two is the most common reason a fix does not work. ## Lowering: rewriting syntax at build time A transpiler parses your source into a syntax tree and prints it back using only constructs the target browsers understand. The rewrite is purely mechanical and happens once, before deployment. | Source syntax | A lowered equivalent | |---|---| | `stop?.platform` | `stop == null ? void 0 : stop.platform` | | `a ?? b` | `a != null ? a : b` (via a temporary variable) | | `class T { count = 0 }` | code in the constructor that initialises `this.count` | | `async function load() {}` | a generator, or a state machine plus a small runtime | Lowering never changes what a method call does. `departures.at(-1)` is already valid syntax in every engine, so the transpiler leaves it exactly as it is, and it fails at run time if the method does not exist. Lowering is also not free. Rewritten code is longer, and some rewrites need **support code**: helper functions for spread or class inheritance, and a generator runtime when `async` code is lowered below generators. That support code ships with the bundle, which is part of why an older target makes output larger. ## Polyfills: supplying built-ins at run time A polyfill is ordinary JavaScript that runs before your code, checks whether a built-in exists and, if not, defines it using only features the old engine already has. Because it uses older code to imitate newer behaviour, a polyfill: - leaves your call sites untouched; `departures.at(-1)` simply starts working; - must run **before** the first call that needs it; - can be **partial**: a `structuredClone` polyfill, for example, cannot clone or transfer every platform type an engine handles natively; - costs bytes and, being plain JavaScript, is usually slower than the native version. A polyfill cannot help with syntax. No runtime script can teach a parser a new token, so a polyfill for `?.` does not exist. ## Reading the crash: which one is missing The error message usually tells you which fix you need: 1. **`SyntaxError` before anything runs** (for example `Unexpected token '?'`): the engine could not parse the file, so none of it executed. Some syntax escaped lowering, either because the target does not include this browser or because a dependency was shipped untransformed. Fix the target or make the build transform that code. 2. **`TypeError: x.at is not a function`** or **`ReferenceError: structuredClone is not defined`** when one line runs: the syntax parsed fine, but a built-in is missing. Add the polyfill, before the code that uses it. 3. **Wrong behaviour but no error**: a polyfill or a lowered construct may differ from native behaviour at an edge; test the edge case in the real browser. ## What neither can fix Some features need the engine itself and cannot be expressed in older code: - **`Proxy`** intercepts arbitrary property access on any object; nothing written in older JavaScript can do that in general, so it can neither be lowered nor fully polyfilled. - **`BigInt`** changes how operators such as `+` and `*` behave and adds a literal form (`10n`); a polyfill cannot change operators, and lowering the literal would not give older engines arbitrary-precision arithmetic. For these, the honest options are to avoid the feature on paths older browsers reach, use a library that offers a different API for the same job, or drop support for that browser. ## Where the two meet Lowering and polyfilling are separate mechanisms, but in a well-configured build they are driven by the **same list of target browsers**: the transpiler lowers only syntax some target lacks, and the polyfill step adds only built-ins some target lacks. When a crash comes in from the field, check the user agent against that list first: a browser outside it is expected to break, while a browser inside it points at a gap in lowering or a missing polyfill.
- Why does unlowered syntax break the whole file instead of just the line that uses it?Because the engine parses a script or module completely before running any of it. A token it does not understand, such as `?.` in an engine that predates it, is a parse error, so nothing in that file runs, and for a module script nothing that depends on it in the module graph runs either. A missing built-in, by contrast, is looked up only when its line executes.
- Can a polyfill be incomplete even when it exists?Yes. A polyfill can only use what the old engine already offers, so some reproduce only part of the standard behaviour. A `structuredClone` polyfill, for example, cannot clone or transfer every platform type the native version handles. Treat a polyfill as an approximation to test in the real browser, not a guarantee of identical behaviour.
- Does lowering async functions add anything besides rewritten syntax?Often it does. Lowering `async` functions below the edition that introduced generators means rewriting each one as a state machine that relies on a small generator runtime, and lowering spread or class inheritance pulls in helper functions. That support code ships with the bundle much like a polyfill, which is one reason an older target produces noticeably larger output.
Lowering is rephrasing a sentence in grammar an older reader already knows; a polyfill is handing that reader a glossary entry for a word they have never seen. If the idea has no words in their language at all, as with Proxy, neither rephrasing nor a glossary helps.
- A polyfill rewrites optional chaining so old browsers can parse it.
- Lowering syntax also adds missing methods like Array.prototype.at.
- If the build succeeds, every target browser can run the output.
- Proxy can be polyfilled like any other missing built-in.
- A SyntaxError in an old browser means a polyfill failed to load.