Explore Polyfill for map, filter and reduce array methods in JavaScript. A detailed explanation of Map, filter and reduce polyfills in JS helps you to know the internal working of these array methods.
Anuj Sharma
Last Updated Oct 2, 2025
If you are preparing for frontend interviews, knowing how to write polyfills is extremely helpful. Interviewers often ask you to re-create commonly used methods like map
, filter
, and reduce
. These questions test not only your JavaScript skills but also how well you understand the prototype chain and higher-order functions.
In JavaScript, methods like map
, filter
, and reduce
are higher-order functions that are widely used in everyday coding. But in interviews, you may be asked to implement these methods yourself. That’s where polyfills come in.
A polyfill is custom code that mimics the behavior of built-in JavaScript functions. By writing polyfills, you understand how JavaScript methods work internally.
Let's first understand how map array method works with an example. The map
method is used to create a new array by applying a callback function to each element.
const numbers = [1, 2, 3];
const doubled = numbers.map(num => num * 2);
console.log(doubled); // [2, 4, 6]
Here are the expected behaviours (or we can say test cases) which we need to handle while implementing map polyfill in JavaScript.
Here is the implementation code for map polyfill in JavaScript
Array.prototype.myMap = function(callback) {
const result = [];
for (let i = 0; i < this.length; i++) {
result.push(callback(this[i], i, this));
}
return result;
};
// Usage
const arr = [1, 2, 3];
const squared = arr.myMap(num => num * num);
console.log(squared); // [1, 4, 9]
Array.prototype
- This will allow to use this new method on any array instance, since this method is attached to the Array Prototype.result
array - this is the new array which we will return as a result.this
)this
represents the array context on which this method will be called, like [1, 2, 3].myMap()
In this example, this represents [1,2,3]
array. (element, index, array)
.The filter
method is used to create a new array with only those elements that satisfy a condition provided as part of the call function in the input.
const numbers = [1, 2, 3, 4];
const evens = numbers.filter(num => num % 2 === 0);
console.log(evens); // [2, 4]
Here are the expected behaviours that we need to handle while implementing the filter polyfill in JS.
Here is the implementation code for the filter polyfill in JavaScript
Array.prototype.myFilter = function(callback) {
const result = [];
for (let i = 0; i < this.length; i++) {
if (callback(this[i], i, this)) {
result.push(this[i]);
}
}
return result;
};
// Usage
const arr = [5, 10, 15, 20];
const greaterThanTen = arr.myFilter(num => num > 10);
console.log(greaterThanTen); // [15, 20]
Array.prototype
.this
represent the array context heretrue
, push the element to the new array.In simple words, reduce
method is used to reduce an array to a single value by executing a callback on each element.
const numbers = [1, 2, 3, 4];
const sum = numbers.reduce((acc, num) => acc + num, 0);
console.log(sum); // 10
Here are the expected behaviours that we need to handle while implementing the reduce polyfill in JS.
Here is the implementation code for reduce polyfill in JavaScript
Array.prototype.myReduce = function(callback, initialValue) {
let accumulator = initialValue !== undefined ? initialValue : this[0];
let startIndex = initialValue !== undefined ? 0 : 1;
for (let i = startIndex; i < this.length; i++) {
accumulator = callback(accumulator, this[i], i, this);
}
return accumulator;
};
// Usage
const arr = [1, 2, 3, 4];
const sum = arr.myReduce((acc, num) => acc + num, 0);
console.log(sum); // 10
accumulator
. If no initial value is passed, use the first element.0
or 1
.(accumulator, currentValue, index, array)
.accumulator
.accumulator
after the loop.Here are more topics to prepare for your next JavaScript Interview
Advertisement
Advertisement
Vijay Sai Krishna vsuri
Last Updated Aug 21, 2025
A Quick guide about popstate event in JavaScript, If you’ve ever hit the back button in your browser and wondered how your Single-Page Application knows which view to render, this guide is for you.
Alok Kumar Giri
Last Updated Jun 2, 2025
Code snippet examples which will help to grasp the concept of Hoisting in JavaScript, with solutions to understand how it works behind the scene.
Anuj Sharma
Last Updated Aug 3, 2025
Explore the implementation of setTimeout in JavaScript with a detailed explanation for every step. Understand all scenarios expected to implement the setTimeout polyfill.
Frontendgeek
Last Updated Sep 25, 2025
Understand the JWT(JSON Web Token) and how JWT decode works. It also covers how the end-to-end JWT authentication works between client & server, along with the pros and cons of using JWT.
Anuj Sharma
Last Updated Sep 14, 2025
Learn hoisting in JavaScript with clear examples and explanations. Understand variable hoisting in JavaScript, function hoisting in JavaScript, and how the temporal dead zone affects hoisting in JS.
Kirtesh Bansal
Last Updated Aug 31, 2025
A beginner-friendly guide to understanding call, apply, and bind methods in JavaScript, along with step-by-step call, apply and bind polyfill implementations that are often asked in interviews.
Subscribe to FrontendGeek Hub for the frontend interview preparation, interview experiences, curated resources and roadmaps.
© 2024 FrontendGeek. All rights reserved