How Reduce Work in JS Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like Report The reduce() method in JavaScript is used to iterate over an array and accumulate a single result based on a callback function. It works by iterating through each element in the array and applying a function that takes the current element and the accumulated value (often called the "accumulator") and returns a new value for the accumulator.array.reduce((accumulator, currentValue, currentIndex, array) => { // return the updated accumulator}, initialValue);Parameter: accumulator: The accumulated value that is returned after each iteration. It can be any type (number, array, object, etc.).currentValue: The current element being processed in the array.currentIndex (optional): The index of the current element being processed.array (optional): The array that reduce() was called on.initialValue (optional): The value to initialize the accumulator. If not provided, the first element of the array is used as the initial value, and the iteration starts from the second element.How It Works Behind the Scenes:Initial Value: If the initialValue is provided, the first iteration uses it as the accumulator. Otherwise, the first element of the array is used as the accumulator, and the iteration starts with the second element.Callback Execution: For each element, the callback function is executed with the accumulator and current element. The callback returns the new accumulator value for the next iteration.Final Result: After all iterations, the final accumulated value is returned. JavaScript const numbers = [1, 2, 3, 4, 5]; const sum = numbers.reduce((accumulator, currentValue) => {\ // Add the current value to the accumulator return accumulator + currentValue; }, 0); // Initial value for the accumulator is 0 console.log(sum); Explanation: First iteration: accumulator = 0, currentValue = 1 → new accumulator = 0 + 1 = 1Second iteration: accumulator = 1, currentValue = 2 → new accumulator = 1 + 2 = 3Third iteration: accumulator = 3, currentValue = 3 → new accumulator = 3 + 3 = 6And so on until the final sum is 15. Comment M meetahaloyx4 Follow 0 Improve M meetahaloyx4 Follow 0 Improve Article Tags : JavaScript Web Technologies 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