Facebook Pixel

Writing a Polyfill for map in JavaScript

A polyfill for map is a classic interview question. Here is how to write one from scratch.

Writing a Polyfill for map in JavaScript

Writing a polyfill for Array.prototype.map is a classic interview question. It tests closures, loops, and understanding of how map works.

What map Does

map takes a callback, calls it on each element, and returns a new array with the results. It does not mutate the original array.

[1, 2, 3].map((n) => n * 2); // [2, 4, 6] `` ### The Polyfill ```js Array.prototype.myMap = function (callback) { const result = []; for (let i = 0; i < this.length; i++) { result.push(callback(this[i], i, this)); } return result; }; `` ### How It Works 1. `this` is the array the method is called on (because it is on `Array.prototype`). 2. Create an empty `result` array. 3. Loop through each element. 4. Call `callback` with `(element, index, array)` (matching the spec). 5. Push the result to `result`. 6. Return `result`. ### Using It ```js [1, 2, 3].myMap((n) => n * 2); // [2, 4, 6] [1, 2, 3].myMap((n, i) => n + i); // [1, 3, 5] `` ### Adding `thisArg` (Optional) The real `map` takes an optional second argument `thisArg` that sets `this` inside the callback: ```js Array.prototype.myMap = function (callback, thisArg) { const result = []; for (let i = 0; i < this.length; i++) { result.push(callback.call(thisArg, this[i], i, this)); } return result; }; `` ### Common Mistakes in Interviews 1. **Using `var` in the loop**: causes the closure bug if the callback is async. Use `let`. 2. **Mutating the original array**: `map` should return a new array, not mutate. 3. **Not passing all three arguments**: `callback` should receive `(element, index, array)`. 4. **Using `forEach` to build the result**: `forEach` returns `undefined`; you need a loop or `reduce`. ### The Takeaway A `map` polyfill: loop through the array, call the callback with `(element, index, array)`, push the result to a new array, and return it. Use `let` in the loop, do not mutate the original, and pass all three arguments to the callback.

Add a method to Array.prototype that loops through the array, calls the callback with (element, index, array), pushes the result to a new array, and returns the new array. Use let in the loop and do not mutate the original.

Three arguments: the current element, the current index, and the array itself. Most callbacks only use the first one, but the polyfill should pass all three to match the spec.

No. map returns a new array with the results. The original array is unchanged. A polyfill should also not mutate the original array.

An optional second argument that sets this inside the callback. Support it with callback.call(thisArg, element, index, array) instead of callback(element, index, array).

Using var in the loop (closure bug with async callbacks), mutating the original array, not passing all three arguments to the callback, and using forEach to build the result (forEach returns undefined).

Ready to master React completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.