skip to content

Transforms & Browser Targets

Transpilers lower new syntax for older browsers while polyfills add missing APIs, both driven by a browser-target query, and minifiers rename code. Interviewers ask which one fixes a crash.

on this pageshow

questions

6

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?

level: juniorimportance: must knowfreq 24%

answer

  1. rewrite versus add
  2. build time versus run time
  3. SyntaxError against is-not-a-function
  4. operators rewritten, built-ins supplied
  5. some features need the engine itself

basics

~20 s

Lowering 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 s

Syntax 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
javascript
// 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 polyfill

go deeper

for a junior

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.

for a middle

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.

for a senior

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.

for a principal

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.
open as a page

How does a browser-target query or a Baseline target decide what a web build lowers and polyfills, and what changes when you move it?

level: middleimportance: should knowfreq 12%

basics

~20 s

The target resolves to minimum browser versions; the transpiler lowers only syntax some listed browser lacks and the polyfill step adds only missing built-ins. An older floor means more rewritten code, more polyfills and larger bundles.

open as a page

What may a minifier rename or remove in a production web build, and why does a check like `err.constructor.name === 'FareError'` start failing?

level: middleimportance: should knowfreq 10%

basics

~20 s

By default a minifier renames local bindings, including function and class names, and strips whitespace, comments and usually debugger statements; it keeps property names and exported names. Renaming a class changes its name property, so name-based checks fail.

open as a page

How does a usage-based polyfill injector choose what a web build adds, and why can it miss a method called through a computed property?

level: middleimportance: should knowfreq 8%

basics

~20 s

It scans each file it compiles for references to built-ins the target browsers lack, matched by name, and adds an import for each. A call like stopsmethod names no method in the source, so nothing is injected for it.

open as a page

When is a second legacy build worth it over one web build lowered for everyone, if the app must also run on older kiosk browsers?

level: seniorimportance: should knowfreq 6%

basics

~20 s

A second legacy build pays off when old browsers are a small but required share: modern browsers keep the lean modern bundle and only kiosks download the lowered, polyfilled copy. Otherwise one lowered build is simpler to ship and test.

open as a page

Why are class decorators a lowering hazard in a TypeScript web build, and how do legacy and standard decorators differ at runtime?

level: seniorimportance: nice to knowfreq 4%

basics

~20 s

The same @decorator syntax has two incompatible meanings: legacy decorators receive a target, key and descriptor, standard ones a value and a context object. Builds must lower the right flavour, and some fast transformers cannot lower standard decorators yet.

open as a page