How to use Array.prototype.reduce() method in JavaScript ? Last Updated : 23 Jul, 2025 Comments Improve Suggest changes 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 Create Quiz Comment S souvikm02 Follow 0 Improve S souvikm02 Follow 0 Improve Article Tags : JavaScript Web Technologies javascript-array JavaScript-Methods JavaScript-Questions +1 More 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 Strings5 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)8 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 readScope of Variables in JavaScript3 min readJavaScript Higher Order Functions7 min readDebugging in JavaScript4 min read Like