The Wayback Machine - https://web.archive.org/web/20240704210340/https://www.geeksforgeeks.org/what-is-a-polyfill/
Open In App

What is a Polyfill ?

Last Updated : 27 Mar, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Polyfill is a way to modify or add new functions. It is basically a piece of code to add/modify the new functions. It is used to provide modern functionality to web browsers.

  • It is basically a piece of code to provide modern functionality to web browsers.
  • It is used to add/modify new functionalities.

It’s like browser fallback what if your browser doesn’t provide the map( ) function then you will need to code your own map( ) function. We will discuss polyfills for the following methods:

  • Using map( )
  • Using forEach( )
  • Using reduce( )

We will code our own map( ), forEach( ) and reduce( ) function. These are all higher-order function which is defined inside Array.prototype so that they are accessible to all the array declared.

For creating our own polyfill we need to declare them inside the Array.prototype.

1. Polyfill for map( ) function

Example: We have been given the array and we need to multiply each element by two.

Javascript




const arr = [1, 2, 3, 4, 5];
  
function callback(ele) {
    return ele * 2;
}
  
Array.prototype.myMap = function (callback) {
    const myArr = [];
    for (const i in this) {
        myArr.push(callback(this[i]));
    }
    return myArr;
};
  
const newArr = arr.myMap(callback);
for (i in newArr) {
    console.log(newArr[i]);
}


Output:

2
4
6
8
10

2. Polyfill for forEach( ) function

Example: Creating our own function like forEach( ) function in JavaScript.

Javascript




const arr = [1, 2, 3, 4, 5];
  
function myFunction(ele) {
    console.log(ele);
}
  
Array.prototype.myForEach = function (callback) {
    for (const i in this) {
        callback(this[i]);
    }
};
  
arr.myForEach(myFunction);


Output:

1
2
3
4
5

3. Polyfill for reduce( ) function

Example: Find the sum of all the even numbers inside the given array.

Javascript




const arr = [1, 2, 3, 4, 5, 6];
  
function callback(ele) {
    if (ele % 2 == 0) {
        return true;
    }
  
    return false;
}
  
Array.prototype.myReduce = function (callback, sum) {
    for (const i in this) {
        if (callback(this[i])) {
            sum += this[i];
        }
    }
    return sum;
};
  
const sum = arr.myReduce(callback, 0);
console.log(sum);


Output:

12

Reference: https://developer.mozilla.org/en-US/docs/Glossary/Polyfill



Similar Reads

Implement polyfill for Array.prototype.map() method in JavaScript
In this article, we will learn how to implement a polyfill for an Array.prototype.map() method in JavaScript. What is a polyfill? A polyfill is a piece of computer code written to implement a feature in a browser that does not yet support it. It could be because of the older version of the browser you are using, or because the new version of the br
3 min read
Implement polyfill for Promise.all() method in JavaScript
The task is to write a polyfill for Promise.all methods in javascript. What is a Polyfill? A polyfill is a piece of computer code written to implement a feature in a browser that does not yet support it. It could be because the older version of the browser you are using, or the new version of the browser does not have that feature. What is Promise.
4 min read
What is the difference between a shim and a polyfill ?
Shim: Shim library in the JavaScript language is the concept that actually blocks API (Application programming interface) calls and also modifies the parameters passed, also handles the operation itself or sometimes redirects the operation to some other place. Shim can be also used for executing programs on various software platforms that they were
2 min read
Implement polyfill for String.prototype.trim() method in JavaScript
In this article, we will learn how to implement a polyfill for a string.prototype.trim() method in JavaScript. A polyfill is a piece of computer code written to implement a feature in a browser that does not yet support it. It could be because of the older version of the browser you are using, or that the new version of the browser does not have th
3 min read
Implement polyfill for Array.prototype.reduce() method in JavaScript
In this article, we will learn how to implement a polyfill for an Array.prototype.reduce() method in JavaScript. A polyfill is a piece of computer code written to implement a feature in a browser that does not yet support it. It could be because of the older version of the browser you are using, or the new version of the browser does not have that
2 min read
How to use polyfill in JavaScript ?
A polyfill in JavaScript is a script that adds modern features to older browsers that do not natively support them. To use it, include the polyfill script in your HTML or install it via a package manager, ensuring compatibility with older environments. Polyfill and its features: Poly means that it could be solved using a wide range of techniques it
2 min read
Polyfill for Array.prototype.filter Method in JavaScript
In JavaScript, the Array.prototype.filter method is the powerful tool for creating a new array containing the elements that meet specific criteria by the callback function. However, in the order environments or browsers, this method might not be available. In such cases, we can create the polyfill to ensure the compatibility. Syntaxarray.filter(cal
3 min read
three90RightbarBannerImg