How Reduce Work in JS Last Updated : 26 Nov, 2024 Summarize Comments Improve Suggest changes Share 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 More infoAdvertise with us Next Article How getElementByID works in JavaScript ? M meetahaloyx4 Follow Improve Article Tags : JavaScript Web Technologies Similar Reads How JavaScript Works? JavaScript is a dynamically typed, cross-platform threaded scripting and programming language, used to put functionality and interactivity at the client side as well as to write logic on the server side of a website. It can display content updates, interactive maps, control multimedia, interactive f 13 min read How closure works in JavaScript ? In this article, we will discuss about the closures working JavaScript. Let us first understand what exactly closures are and basic details which are associated with closures in JavaScript.A Closure is a combination of a function enclosed with references to its surrounding state (the lexical environ 2 min read How does memory stacks work in Javascript ? Introduction: The memory stack is a mechanism in JavaScript that allows us to allocate and free memory. If a programmer wants to store some data, they must first create an empty part of the heap that is called a "stack." Then, they can push and then pop values on the stack. When working with strings 3 min read How getElementByID works in JavaScript ? The document method getElementById() returns an element object representing the element whose id property matches with the given value. This method is used to manipulate an element on our document & is widely used in web designing to change the value of any particular element or get a particular 2 min read Underscore.js _.each() Function Underscore.js is a JavaScript library that provides a lot of useful functions that helps in the programming in a big way like the map, filter, invoke etc even without using any built-in objects. The _.each() function is an inbuilt function in Underscore.js library of JavaScript which is used to retu 3 min read How to trigger events in JavaScript ? JavaScript is a high-level, interpreted, dynamically typed client-side scripting language. While HTML is static and defines the structure of a web page, JavaScript adds interactivity and functionality to HTML elements. This interaction is facilitated through events, which are actions or occurrences 2 min read Like