JavaScript Interview Question #165

What is a polyfill?

Performance, Browser APIs, Rendering & Modern Web Architecture Senior Advanced

Quick Interview Answer

A polyfill supplies an implementation of a web or JavaScript API that is missing from a target environment. It differs from transpilation, which primarily rewrites syntax.

Detailed Explanation

A polyfill supplies an implementation of a web or JavaScript API that is missing from a target environment. It differs from transpilation, which primarily rewrites syntax.

For example, a build may transpile newer syntax while conditionally including polyfills for runtime features required by older browsers. Polyfills should be selected based on real compatibility requirements to avoid unnecessary bundle size.

Code Example

// Conceptual feature check:
if (!Array.prototype.someFeature) {
    // A standards-compatible polyfill could define it here.
}

// In real projects, use maintained tooling/polyfill packages
// rather than inventing incomplete implementations.