Polyfill for map, filter, and reduce in JavaScript
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 Feb 21, 2026

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.
Table of Contents
- Polyfill for map, filter, and reduce in JavaScript
- map polyfill in JavaScript
- filter polyfill in JavaScript
- reduce polyfill in JavaScript
- Learn More
Polyfill for map, filter, and reduce in JavaScript
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.
map() polyfill in JavaScript
Understand the map array method in JavaScript
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.
Example:
const numbers = [1, 2, 3];
const doubled = numbers.map(num => num * 2);
console.log(doubled); // [2, 4, 6]
Expected map method behaviour
Here are the expected behaviours (or we can say test cases) which we need to handle while implementing map polyfill in JavaScript.
- It should take a callback function as input.
- It should call the callback for each element.
- It should return a new array.
Map polyfill code in JavaScript(ES6) with example
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]
Step-by-Step Polyfill Explanation
- Attach a new method to
Array.prototype- This will allow to use this new method on any array instance, since this method is attached to the Array Prototype. - Start with empty
resultarray - this is the new array which we will return as a result. - Loop through the array (
this)
Here,thisrepresents the array context on which this method will be called, like[1, 2, 3].myMap()In this example, this represents[1,2,3]array. - Call the callback with each element
(element, index, array). - Push the result into a new array.
- Return the new array.
filter polyfill in JavaScript
Understand the filter array method in JavaScript
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.
Example:
const numbers = [1, 2, 3, 4];
const evens = numbers.filter(num => num % 2 === 0);
console.log(evens); // [2, 4]
Expected filter method behaviour
Here are the expected behaviours that we need to handle while implementing the filter polyfill in JS.
- It should take a callback function.
- It should return a new array with elements that pass the condition (provided as part of the callback function).
- The original array should not be modified.
Filter polyfill code in JavaScript(ES6) with an example
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]
Step-by-Step Polyfill Explanation
- Attach a new method to
Array.prototype. - Loop through the array.
thisrepresent the array context here - Call the callback.
- If it returns
true, push the element to the new array. - Return the new array.
reduce polyfill in JavaScript
Understand the reduce array method in JavaScript
In simple words, reduce method is used to reduce an array to a single value by executing a callback on each element.
Example:
const numbers = [1, 2, 3, 4];
const sum = numbers.reduce((acc, num) => acc + num, 0);
console.log(sum); // 10
Expected reduce method behaviour
Here are the expected behaviours that we need to handle while implementing the reduce polyfill in JS.
- It should take a callback function and an optional initial value.
- It should apply the callback to each element.
- It should return a single accumulated result.
Reduce polyfill code in ES6 with an example
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
Step-by-Step Polyfill Explanation
- Define
accumulator. If no initial value is passed, use the first element. - Start looping from index
0or1. - Call the callback with
(accumulator, currentValue, index, array). - Update the
accumulator. - Return the
accumulatorafter the loop.
Learn More
Here are more topics to prepare for your next JavaScript Interview
A seasoned Sr. Engineering Manager at GoDaddy (Ex-Dell) with over 12+ years of experience in the frontend technologies. A frontend tech enthusiast passionate building SaaS application to solve problem. Know more about me 🚀
Learn Next
Comments
Be the first to share your thoughts!
No comments yet.
Start the conversation!
Share your expertise
Publish a blog or quick notes on topics you know well — your write-up could be the answer someone needs before their next frontend interview.
Build your portfolio
Help the community
Sharpen your skills
Earn goodies
Other Related Blogs
Understand Debouncing in JavaScript with Examples
Anuj Sharma
Last Updated Jun 27, 2026
Understand the concept of Debouncing in JavaScript to improve the performance of your web application and optimize the event handling in JavaScript, by limiting API calls or DOM events.
Implement JSON Parse polyfill in JavaScript
Anuj Sharma
Last Updated Jun 15, 2026
Explore the code implementation of JSON Parse Polyfill in JavaScript that can parse a string into valid JSON.
Implement JSON Stringify Polyfill in JavaScript
Anuj Sharma
Last Updated Jun 15, 2026
Explore the detailed explanation of the JSON Stringify Polyfill implementation in JavaScript to prepare for the frontend interviews.
What is javascript:void(0) and How it Works?
Anuj Sharma
Last Updated Jun 15, 2026
A comprehensive explanation about using javascript:void(0) in javascript. When to use javascript:void(0) and how it works with examples of using it with anchor tag.
Understand JavaScript Date Object with Examples (for JavaScript Interviews)
Anuj Sharma
Last Updated Jun 15, 2026
Go through different ways to display dates using javascript date object. It covers examples of date object usage to understand the main concepts of javascript date object.
Apply Polyfill in JavaScript: Step by Step Explanation (For Interview)
Anuj Sharma
Last Updated Jun 15, 2026
Understand how the apply method works in JavaScript and cover step by step-by-step explanation of the apply method polyfill in JavaScript to understand its internal implementation.
Memory Leak in JavaScript: 4 Most Common Reasons
Anuj Sharma
Last Updated Jun 15, 2026
A quick guide to understanding the most common reasons and how to avoid Memory Leak in JavaScript with examples to build more robust web applications.
How to Format Phone Number in JavaScript (JavaScript Interview)
Anuj Sharma
Last Updated Jun 15, 2026
Learn the best & quickest way to format phone number in JavaScript with or without country codes. This will help websites to show the phone numbers in a more human-readable format.
