Implement polyfill for Array.prototype.reduce() method in JavaScript Last Updated : 01 Aug, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 feature. We will discuss the reduce() method supported by arrays in JavaScript, and implement our own version of it as well. The reduce() method calls a callback function, called a reducer function, on every element of the array one-by-one, and returns a single value as an output. Example: JavaScript <script> const arr = [1, 2, 3, 4]; // Find sum of array elements // using reduce method const sum = arr.reduce((total, num) => total + num); console.log(sum); </script> Output: 10 Array.prototype.reduce(): Let us create our own simple polyfill for the reduce() method. Example: We created a "myReduce" prototype method to work in the same way as the reduce() methodThe 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. JavaScript <script> Array.prototype.myReduce = function (callback, initialValue) { // Variable that accumulates the result // after performing operation one-by-one // on the array elements let accumulator = initialValue; for (let i = 0; i < this.length; i++) { // If the initialValue exists, we call // the callback function on the existing // element and store in accumulator if (accumulator) { accumulator = callback.call(undefined, accumulator, this[i], i, this); // If initialValue does not exist, // we assign accumulator to the // current element of the array } else { accumulator = this[i]; } } // We return the overall accumulated response return accumulator; } // Code to calculate sum of array elements // using our own reduce method const arr = [1, 2, 3, 4]; console.log(arr.myReduce((total, elem) => total + elem)); // Code to calculate multiplication of all array elements console.log(arr.myReduce((prod, elem) => prod * elem)); </script> Output: 10 24 polyfills- Reduce Visit Course Comment More infoAdvertise with us Next Article Polyfill for Array.prototype.filter Method in JavaScript D dishebhbhayana Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions 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 y 4 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 t 3 min read How to use Array.prototype.reduce() method in JavaScript ? The Array.prototype.reduce() method is used in an array to return a single value from an array after executing the user-supplied callback function on each array element variety. It transverses from the left-most-element to the right-most-element of the given array.Array.prototype.reduce() can be cal 2 min read JavaScript Array reduce() Method The JavaScript Array.reduce() method iterates over an array, applying a reducer function to each element, accumulating a single output value. It takes an initial value and processes elements from left to right, reducing the array to a single result. It is useful for doing operations like max in an a 3 min read Important Array Methods of JavaScript JavaScript arrays are powerful tools for managing collections of data. They come with a wide range of built-in methods that allow developers to manipulate, transform, and interact with array elements.Some of the most important array methods in JavaScript areTable of Content1. JavaScript push() Metho 7 min read How to define custom Array methods in JavaScript? JavaScript arrays are versatile data structures used to store collections of items. When you want to iterate through the elements of an array, it's common to use loops like for, for...of, or forEach. However, there's a nuance when it comes to iterating through arrays that have additional properties 2 min read Like