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.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe 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
- What is a polyfill? Polyfill vs transpiler?
- Write String.prototype.startsWith and explain each line. (Flipkart, 2025)
- Why is extending built-in objects a bad idea?
- Change the behaviour of Array.prototype.sort through the prototype (Swiggy OA, 2025). How, and why is it risky?
- How do you decide which polyfills to ship?
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.