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
Anuj Sharma
Last Updated Nov 3, 2025
Find the top React Performance Optimization Techniques specific to React applications that help to make your react app faster and more responsive for the users along with some bonus techniques.
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 Nov 3, 2025
Find the step-by-step explanation of the useFetch custom hook in React that helps in fetching the data from an API and handling loading, error states.
Anuj Sharma
Last Updated Oct 26, 2025
In this post, we will going to cover the step-by-step implementation of Infinite Currying Sum with a code example. This is one of the most common JavaScript Interview questions.
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.
Anuj Sharma
Last Updated Oct 26, 2025
Understand the step-by-step implementation of Infinite Currying Multiplication in JavaScript with a code example.
Subscribe to FrontendGeek Hub for the frontend interview preparation, interview experiences, curated resources and roadmaps.
© 2024 FrontendGeek. All rights reserved