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 feature.
Array.prototype.reduce(): Let us create our own simple polyfill for the reduce() method.
- We created a “myReduce” prototype method to work in the same way as the reduce() method
- The myReduce() method takes 2 arguments, a callback function, and an initialValue.
- We run a loop across all the array elements.
- We check if the initialValue exists, if it exists, we call the callback function on the accumulator and the array element, and store the result in the accumulator, else we initialize the accumulator with the array element.
- We return the accumulator as the final result.