JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionsjavascript
Prev

Learn the concept

Arrays

javascript
junior
polyfills

What are polyfills and how do you implement Array.prototype.map from scratch?

polyfill
arrays
prototype
map
filter
reduce
fundamentals
Quick Answer

A polyfill is code that provides modern functionality in older environments that don't natively support it. Implementing Array.prototype.map requires iterating over the array, calling the callback with each element, index, and the original array, and collecting results into a new array.

Detailed Explanation

A polyfill is a piece of code (usually JavaScript) that provides the implementation of a feature on browsers or environments that do not support it natively. The term was coined by Remy Sharp in 2010 — it "fills in" the gaps in older environments so your code works everywhere.

How Polyfills Work

Polyfills use feature detection to check if a feature already exists before adding it:

JavaScript
if (!Array.prototype.includes) {
  Array.prototype.includes = function (searchElement, fromIndex) {
    // implementation
  };
}

This pattern ensures the polyfill only runs when needed and doesn't override native implementations (which are typically faster).

Polyfill vs Transpiler

  • Polyfill: Adds missing APIs at runtime (e.g., Promise, Array.prototype.flat, Object.entries). Works by adding to prototypes or globals.
  • Transpiler (like Babel): Transforms new syntax into older syntax at build time (e.g., arrow functions → regular functions, class → prototype-based). Cannot polyfill new APIs — only transforms syntax.

You often need both: Babel transpiles syntax, and a polyfill library like core-js fills in missing APIs.

Implementing Array.prototype.map

The native map() creates a new array by calling a callback function on every element. A correct implementation must:

  1. Handle this context (the array)
  2. Pass three arguments to the callback: element, index, and the original array
  3. Support an optional thisArg for the callback's this binding
  4. Skip holes in sparse arrays (preserve them in the result)
  5. Return a new array (never mutate the original)

Common Polyfill Interview Questions

Interviewers frequently ask candidates to implement:

  • Array.prototype.map — transform each element
  • Array.prototype.filter — keep elements matching a condition
  • Array.prototype.reduce — accumulate a value from all elements
  • Function.prototype.bind — create a bound function with preset this
  • Promise — asynchronous value with chaining
  • Array.prototype.flat — flatten nested arrays

These test understanding of how JavaScript built-ins actually work under the hood.

Code Examples

Implementing Array.prototype.mapJavaScript
// Polyfill for Array.prototype.map
if (!Array.prototype.myMap) {
  Array.prototype.myMap = function (callback, thisArg) {
    if (typeof callback !== 'function') {
      throw new TypeError(callback + ' is not a function');
    }

    const result = new Array(this.length);

    for (let i = 0; i < this.length; i++) {
      // Skip holes in sparse arrays
      if (i in this) {
        result[i] = callback.call(thisArg, this[i], i, this);
      }
    }

    return result;
  };
}

// Usage
const nums = [1, 2, 3, 4];
const doubled = nums.myMap((n) => n * 2);
console.log(doubled); // [2, 4, 6, 8]

// With thisArg
const multiplier = { factor: 10 };
const scaled = nums.myMap(function (n) {
  return n * this.factor;
}, multiplier);
console.log(scaled); // [10, 20, 30, 40]

Real-World Applications

Use Cases

Cross-Browser Compatibility

E-commerce sites like Flipkart polyfill modern APIs to support older browsers still used by a significant portion of their user base in emerging markets.

Build Pipeline Integration

Projects use core-js with Babel to automatically include only the polyfills needed based on target browser versions specified in browserslist configuration.

Mini Projects

Polyfill Collection

intermediate

Implement polyfills for Array.prototype.flat, Array.prototype.find, Object.entries, and Function.prototype.bind. Write tests to verify they match native behavior.

Industry Examples

Babel

Babel's @babel/preset-env integrates with core-js to automatically inject only the polyfills your target browsers need, keeping bundle size minimal.

Resources

MDN - Array.prototype.map()

docs

MDN - Polyfill

docs

core-js - Standard JavaScript polyfill library

docs

Related Questions

What is the difference between map(), filter(), and reduce() array methods?

junior
arrays

Explain JavaScript's prototype chain and how inheritance works.

senior
prototypes

What are call, apply, and bind and how do they differ?

mid
this
Previous
What is an IIFE (Immediately Invoked Function Expression) and why is it used?
Prev