Skip to the page

Polyfills interviewers ask · mid

Polyfill ground rules (and why not to extend built-ins)

A polyfill adds a missing standard API with exactly the spec's behaviour, only when it is missing, as a non-enumerable property — and you should never add your own non-standard methods to built-in prototypes, because they collide with future standards and other libraries.

The notes

What you must know

  • Polyfill (adds an API) vs transpile (rewrites syntax). In production: core-js with Babel or a bundler, driven by browserslist and usage.
  • Feature-detect first: if (!String.prototype.startsWith) { ... }.
  • Define with Object.defineProperty(proto, name, { value, writable: true, configurable: true, enumerable: false }). An enumerable method shows up in every for...in loop.
  • Use a normal function (not an arrow) so this is the receiver; convert this properly (String(this)); validate callbacks (throw TypeError); clamp positions and lengths.
  • Follow the spec's edge cases: holes, negative indexes, NaN, missing arguments.
  • Risks of extending built-ins: SmooshGate (a library's Array.prototype.flatten forced the standard to be named flat), clashes between libraries, and prototype pollution.
  • In interviews name it myX (or write a standalone function) and say you'd use core-js in production.

The red pen

Common traps: mistake → fix

  • Mistake: Assigning the method directly

    Fix: it becomes enumerable and breaks for...in.

  • Mistake: Arrow functions for prototype methods

    Fix: this is wrong.

  • Mistake: Polyfilling everything for every user

    Fix: bundle size; target real browsers from analytics.

Easy to hard

Questions interviewers ask, easy to hard

  1. What is a polyfill? Polyfill vs transpiler?
  2. Write String.prototype.startsWith and explain each line. (Flipkart, 2025)
  3. Why is extending built-in objects a bad idea?
  4. Change the behaviour of Array.prototype.sort through the prototype (Swiggy OA, 2025). How, and why is it risky?
  5. How do you decide which polyfills to ship?

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview