Implement polyfill for Array.prototype.reduce() method in JavaScript Last Updated : 23 Jul, 2025 Comments Improve Suggest changes 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 info D dishebhbhayana Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Explore JavaScript BasicsIntroduction to JavaScript4 min readVariables and Datatypes in JavaScript6 min readJavaScript Operators5 min readControl Statements in JavaScript4 min readArray & StringJavaScript Arrays7 min readJavaScript Array Methods7 min readJavaScript Strings6 min readJavaScript String Methods9 min readFunction & ObjectFunctions in JavaScript5 min readJavaScript Function Expression3 min readFunction Overloading in JavaScript4 min readObjects in Javascript4 min readJavaScript Object Constructors4 min readOOPObject Oriented Programming in JavaScript3 min readClasses and Objects in JavaScript4 min readWhat Are Access Modifiers In JavaScript ?5 min readJavaScript Constructor Method7 min readAsynchronous JavaScriptAsynchronous JavaScript2 min readJavaScript Callbacks4 min readJavaScript Promise4 min readEvent Loop in JavaScript4 min readAsync and Await in JavaScript2 min readException HandlingJavascript Error and Exceptional Handling6 min readJavaScript Errors Throw and Try to Catch2 min readHow to create custom errors in JavaScript ?2 min readJavaScript TypeError - Invalid Array.prototype.sort argument1 min readDOMHTML DOM (Document Object Model)9 min readHow to select DOM Elements in JavaScript ?3 min readJavaScript Custom Events4 min readJavaScript addEventListener() with Examples9 min readAdvanced TopicsClosure in JavaScript4 min readJavaScript Hoisting6 min readJavascript Scope3 min readJavaScript Higher Order Functions7 min readDebugging in JavaScript4 min read Like