How to use Array.prototype.reduce() method in JavaScript ? Last Updated : 19 Nov, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 called using two ways.Callback function: The function will be declared first and later called for execution on an array.Syntaxarray.reduce( function(returnValue, currentValue, currentIndex, array), initialValue )Inline callback function: The parameters are passed inside the function inside reduce() method along with the array that is to be executed. It accepts four arguments:returnValue: It is the returning value resulting from the previous call of the callback function. On its first call, it takes the value of initialValue else takes the value of the first element of the array, i.e. array[0]currentValue: It is the present value of the element which is getting currently executed by the callback function. On the first call, it is the first element of the array if initialValue was specified else it is array[1], i.e. a second element of the array.currentIndex: It is the value of the index of the 'currentValue' element. It takes the value '0' if initialValue is specified else '1'.array: It is the required array we need to transverse.initialValue: It is an optional parameter that will be initialized as returnValue if it is mentioned, after which execution will be performed on other elements of the array starting from index 0.Example 1: We will see the basic use of the Array.prototype.reduce() method using the callback function in Javascript. JavaScript const array = [10, 18, 30, 41, 60]; const myReducer = (returnValue, currentValue) => returnValue + currentValue; // 10 + 18 + 30 + 41 + 60 console.log(array.reduce(myReducer)); // expected output: 159 // initialValue = 20 // 20 + 10 + 18 + 30 + 41 + 60 console.log(array.reduce(myReducer, 20)); // expected output: 179 Output159 179 Example 2: We will see the use of the Array.prototype.reduce() method in Javascript. JavaScript const array1 = [1, 2, 3, 4, 5]; // Calculating sum of squares const myReducer = (returnValue, currentValue) => returnValue + currentValue * currentValue; // 1 + 4 + 9 + 16 + 25 console.log(array1.reduce(myReducer)); // expected output: 55 // initialValue = 6 // 36 + 1 + 4 + 9 + 16 + 25 console.log(array1.reduce(myReducer, 36)); // expected output: 91 Output55 91 Comment More infoAdvertise with us Next Article JavaScript Array reduceRight() Method S souvikm02 Follow Improve Article Tags : JavaScript Web Technologies javascript-array JavaScript-Methods JavaScript-Questions +1 More Similar Reads 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 2 min read How to use map() filter() and reduce() in JavaScript? The map(), filter(), and reduce() are the array functions that allow us to manipulate an array according to our logic and return a new array after applying the modified operations on it. 1. JavaScript map() MethodThe map() method in JavaScript is used to create a new array by applying a function to 2 min read How to Truncate an Array in JavaScript? Here are the different methods to truncate an array in JavaScript1. Using length PropertyIn Array.length property, you can alter the length of the array. It helps you to decide the length up to which you want the array elements to appear in the output.JavaScriptconst n = [1, 2, 3, 4, 5, 6]; n.length 4 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 JavaScript Array reduceRight() Method The Javascript arr.reduceRight() method in JavaScript is used to convert elements of the given array from right to left to a single value.Syntax: array.reduceRight( function(total, currentValue, currentIndex, arr), initialValue )Parameters: This method accepts five parameters as mentioned above and 3 min read How does Array.prototype.slice.call work in JavaScript ? Array.prototype.slice.call()Array.prototype.slice.call() is a method in JavaScript, It is used to convert an array-like or iterable object into a genuine array. It allows you to extract a portion of an array or an array-like object and return a new array. This technique is often used to manipulate a 2 min read Like