![]() |
VOOZH | about |
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'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:
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.
Output:
2 4 6 8 10
2. Polyfill for forEach( ) function
Example: Creating our own function like forEach( ) function in JavaScript.
Output:
1 2 3 4 5
3. Polyfill for reduce( ) function
Example: Find the sum of all the even numbers inside the given array.