What is a polyfill, and how is it different from a transpiler?
IntermediateJavaScript interview questionBy UIRound, updated 17 November 2026
Short answer
A polyfill is JavaScript that adds a missing built-in API, such as Array.prototype.at or structuredClone, when the browser lacks it. It runs in the browser at runtime. A transpiler rewrites code you wrote with new syntax, such as ?. or class fields, into older syntax at build time. Polyfills fix missing features; transpilers fix code the old engine cannot even parse.
Two different problems
New JavaScript features come in two kinds, and each needs a different tool.
| Polyfill | Transpiler | |
|---|---|---|
| Fixes | Missing APIs: functions, methods, objects | New syntax: operators, keywords, grammar |
| Examples | Array.prototype.at, Object.hasOwn, Promise.withResolvers |
a?.b, a ?? b, class fields, async/await |
| When it runs | In the browser, at runtime | On your machine or CI, at build time |
| How it works | Checks if the API exists and defines it if not | Parses your code and prints equivalent older code |
| Typical tools | core-js, single-purpose packages |
Babel, SWC, esbuild, TypeScript's tsc |
| Output | Extra code shipped to the browser | Your code, rewritten |
What a polyfill looks like
if (!Array.prototype.at) {
Object.defineProperty(Array.prototype, "at", {
value: function at(index) {
const length = this.length >>> 0;
let i = Math.trunc(index) || 0;
if (i < 0) i += length;
return i < 0 || i >= length ? undefined : this[i];
},
writable: true,
configurable: true,
});
}This is simplified, but it shows the three things a good polyfill does:
- Feature detection first. It only defines the method if it is missing, so browsers with a native version keep it. See feature detection vs user agent sniffing.
- Matches the spec.
at(-1)returns the last item,at(1.7)treats the index as1, and out-of-range reads returnundefined, just like the real method. Code written for the native version must work unchanged. - Defines the property like a built-in.
definePropertykeeps it non-enumerable, so it does not show up infor...inloops over arrays. Plain assignment would make it enumerable.
Patching built-ins is normally a bad idea, as explained in why you should not extend built-in prototypes. Polyfills are the accepted exception, because they only add standard APIs with standard behaviour.
What a transpiler does
// You write:
const city = user?.address?.city ?? "Unknown";
// A transpiler targeting old browsers outputs roughly:
var _user$address, _ref;
var city =
(_ref = user === null || user === void 0
? void 0
: (_user$address = user.address) === null || _user$address === void 0
? void 0
: _user$address.city) !== null && _ref !== void 0
? _ref
: "Unknown";The transpiler reads your source as a syntax tree and prints the same logic using only syntax the target understands. Nothing new is added to the browser. The Babel answer covers how that pipeline and its plugins work.
Why syntax cannot be polyfilled
A browser parses a whole script before running any of it. If the file contains ?. and the engine does not know that operator, parsing fails with a SyntaxError and not a single line runs, including any polyfill in the same file. A polyfill is code, and code cannot run until the parser accepts it. You cannot teach a parser new grammar from inside the program.
APIs are different. [1, 2].at(-1) is valid old syntax: a method call. It only fails at runtime because at is undefined, and a polyfill that runs earlier can fill that gap.
Some features sit between the two. Proxy cannot be fully polyfilled or transpiled, because it changes how basic operations on objects work. BigInt literals like 10n are syntax and the type has no full polyfill. Regular expression lookbehind cannot be rewritten in general. For these, you either target engines that support them or avoid them.
core-js and browserslist
You rarely write polyfills by hand. core-js is the standard library of them, covering most of ECMAScript. Babel's preset-env with useBuiltIns: "usage" adds core-js imports only for the APIs your code uses, and SWC has a similar option.
Both the transpiler and the polyfill step read one list of target browsers from browserslist, in package.json or a .browserslistrc file:
> 0.5%, last 2 versions, not deadRun npx browserslist to print the browsers a query matches. The wider the list, the more syntax gets rewritten and the more polyfills ship. A list of modern browsers can mean almost nothing is transformed.
Framework defaults help. Next.js targets modern browsers out of the box and injects a few common polyfills, such as fetch and URL, only for browsers that need them. Anything else you import yourself; in the App Router the documented place is instrumentation-client.ts. Vite has @vitejs/plugin-legacy when you must support older browsers.
When you still need them
- Transpiling is almost always on, because you write TypeScript or JSX, which browsers do not run. Lowering modern syntax is a setting on the same step.
- Polyfills matter when your audience includes older browsers, embedded webviews or smart TVs, or when you use APIs newer than your targets. Check real usage data before dropping support.
- Node.js code usually needs neither for its runtime APIs, since you control the version, but published libraries should state which versions they support.
Self-host polyfills or bundle them from npm instead of loading them from a third-party CDN. In 2024 the polyfill.io domain changed owners and began serving malicious code to the sites that loaded it.
Practise it
Build a polyfill loader that feature-detects and loads only what is missing is the system-level exercise. For spec-accurate polyfills, try Polyfill toSorted, toReversed, toSpliced and with, Write an Array.isArray polyfill, Write Object.entries, Object.fromEntries and Object.assign polyfills and Polyfill Promise.prototype.finally.
Common follow-up questions
What is a ponyfill?
A ponyfill provides the same behaviour as a function you import, without touching globals or prototypes: you write at(list, -1) instead of list.at(-1). It is safer in libraries, because it cannot clash with other code or a future native version.
Is a transpiler the same as a compiler?
A transpiler is a compiler whose output is source code in a similar language, usually JavaScript to older JavaScript. The words are often used interchangeably.
Can I load polyfills only in browsers that need them?
Yes. Feature-detect at startup and dynamically import() the polyfill if the check fails, before the code that depends on it runs. The cost is an extra request for old browsers, while modern ones download nothing.
Does TypeScript add polyfills?
No. tsc lowers syntax to the target you set, but it never adds missing APIs. Setting lib to a newer version only changes the types it allows; it does not make those APIs exist at runtime.
Mistakes to avoid
Assuming the transpiler also adds missing APIs
Rewriting syntax and adding built-ins are separate steps.
Putting a polyfill in the same file as the new syntax it is meant to protect
Defining a polyfill with plain assignment, which makes it enumerable, or without checking for the native version
Not transpiling dependencies that ship modern syntax when you support old browsers
Loading polyfills from a third-party CDN you do not control
Sources
Practise it in code
Interviewers rarely stop at the definition. These questions open in an editor with tests.
- Build a polyfill loader that feature-detects and loads only what is missing, in dependency orderHard60 min
- Polyfill
toSorted,toReversed,toSplicedandwith: change-array-by-copy with holes, negative indexes and no mutationEasy25 min - Write an
Array.polyfill withoutisArray toString, and predict whereinstanceof Arrayfails across iframesEasy25 min - Write
Object.,entries Object.andfromEntries Object.polyfills: getters, symbols and own enumerable keysEasy15 minassign - Polyfill
Promise.with correct pass-through semanticsMedium25 minprototype. finally