A cleaner way to accomplish this is by providing an initial value as the second argument to reduce:

Copyvar arr = [{x:1}, {x:2}, {x:4}];
var result = arr.reduce(function (acc, obj) { return acc + obj.x; }, 0);
console.log(result);  // 7
Run code snippetEdit code snippet Hide Results Copy to answer Expand

The first time the anonymous function is called, it gets called with (0, {x: 1}) and returns 0 + 1 = 1. The next time, it gets called with (1, {x: 2}) and returns 1 + 2 = 3. It's then called with (3, {x: 4}), finally returning 7.

This also handles the case where the array is empty, returning 0.

Answer from Casey Chu on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › reduce
Array.prototype.reduce() - JavaScript | MDN
reduce() is a central concept in functional programming, where it's not possible to mutate any value, so in order to accumulate all values in an array, one must return a new accumulator value on every iteration. This convention propagates to JavaScript's reduce(): you should use spreading or ...
🌐
W3Schools
w3schools.com › jsref › jsref_reduce.asp
JavaScript Array reduce() Method
The reduce() method executes a reducer function for array element.
Discussions

javascript - How to call reduce on an array of objects to sum their properties? - Stack Overflow
3 - Respect the Functors -- JavaScript shines best when its inner functional child is unleashed. In the functional world, there is a standard on how you "fold" or reduce an array. When you fold or apply a catamorphism to the array, you take the values of that array to construct a new type. More on stackoverflow.com
🌐 stackoverflow.com
How to use the JavaScript Array.prototype.reduce() - JavaScript Reduce Explained with Examples
The reduce() method applies a function against an accumulator and each value of the array (from left-to-right) to reduce it to a single value. Syntax arr.reduce(callback[, initialValue]) Parameters callback Function to execute on each value in the array, taking four arguments: previousValue ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
18
September 8, 2019
Learning array.reduce() method, i need some help to understand it.
Best way is to play with it. Take a look at the docs, they're easy to understand. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce More on reddit.com
🌐 r/learnjavascript
68
64
April 12, 2022
javascript - What does the Array method `reduce` do? - Stack Overflow
I found a very useful function reduce, and I'm using it, but I'm not sure if I understand it properly. Can anyone help me to understand this function? Example: var arr = [ 1, 2, 3, 4, 5, 6 ]; arr.r... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Reddit
reddit.com › r/learnjavascript › can anyone explain to me easily .reduce in js !
r/learnjavascript on Reddit: Can anyone explain to me easily .reduce in js !
August 2, 2022 -

I own searched online but I do not understand‏‏‎‏‏‎‏‏‎‏‏‎­things like accumalator or reducer, I understand .map and .filter but not .reduce. If you could an analogy would be appreciated.

Top answer
1 of 5
17
Reduce is the most low-level of these array iteration methods, so it has the widest variety of potential uses and requires the most complex boiler plate to write. That makes it hard to sum up in a concise way. You often hear people describe reduce as "reducing an array to a single value" or something like that, but I find that definition both hard to understand and kind of inaccurate (reduce can produce another array!). So I don't really try to sum up reduce. I just think of it purely in mechanical terms. Imagine we wrote our own version. function reduce(items, iteratee, initial) { let accumulator = initial; for (const item of items) { accumulator = iteratee(accumulator, item); } return accumulator; } We can then use this reduce just like the built in one. function add(x, y) { return x + y; } const nums = [1, 2, 3]; const sum = reduce(nums, add, 0); console.log(sum); // 6 So what does reduce do? It calls a function on each item in an array, passing along both the result of the previous call and the item itself, before finally returning the result of the final call. It defies any less mechanical definition because it is a fiddly mechanical function with only fiddly technical use cases. EDIT: clarified wording
2 of 5
3
you use .map() to create a new array of the same length as the input array you use .filter() to create a new array containing a subset of the input array you use .reduce() to create something entirely new using an array as an input For example, if we have const lovers = ["mindy", "jed", "jerry"]; const sentence = lovers.reduce((phrase, name) => `${name} loves ${phrase}`); console.log(sentence); // jerry loves jed loves mindy We have transformed an array into a string! I just pulled this monkey-island-esque example out my ass, but you get the gist; use .reduce() when you wanna take an array of things, and generate something entirely new using them.
🌐
DEV Community
dev.to › benlesh › understanding-array-reduce-by-building-it-from-scratch-1fk7
Understanding Array reduce by building it from scratch - DEV Community
January 1, 2020 - Why is it called "reduce"? I'm not actually sure here. But how I remember what it does is that, generally, you're taking the array and you're "reducing it down" to something else. Now, this is a bit of a misnomer still, because you could use reduce to make a new, larger, array.
Top answer
1 of 16
496

A cleaner way to accomplish this is by providing an initial value as the second argument to reduce:

Copyvar arr = [{x:1}, {x:2}, {x:4}];
var result = arr.reduce(function (acc, obj) { return acc + obj.x; }, 0);
console.log(result);  // 7
Run code snippetEdit code snippet Hide Results Copy to answer Expand

The first time the anonymous function is called, it gets called with (0, {x: 1}) and returns 0 + 1 = 1. The next time, it gets called with (1, {x: 2}) and returns 1 + 2 = 3. It's then called with (3, {x: 4}), finally returning 7.

This also handles the case where the array is empty, returning 0.

2 of 16
414

After the first iteration your're returning a number and then trying to get property x of it to add to the next object which is undefined and maths involving undefined results in NaN.

try returning an object contain an x property with the sum of the x properties of the parameters:

Copyvar arr = [{x:1},{x:2},{x:4}];

arr.reduce(function (a, b) {
  return {x: a.x + b.x}; // returns object with property x
})

// ES6
arr.reduce((a, b) => ({x: a.x + b.x}));

// -> {x: 7}

Explanation added from comments:

The return value of each iteration of [].reduce used as the a variable in the next iteration.

Iteration 1: a = {x:1}, b = {x:2}, {x: 3} assigned to a in Iteration 2

Iteration 2: a = {x:3}, b = {x:4}.

The problem with your example is that you're returning a number literal.

Copyfunction (a, b) {
  return a.x + b.x; // returns number literal
}

Iteration 1: a = {x:1}, b = {x:2}, // returns 3 as a in next iteration

Iteration 2: a = 3, b = {x:2} returns NaN

A number literal 3 does not (typically) have a property called x so it's undefined and undefined + b.x returns NaN and NaN + <anything> is always NaN

Clarification: I prefer my method over the other top answer in this thread as I disagree with the idea that passing an optional parameter to reduce with a magic number to get out a number primitive is cleaner. It may result in fewer lines written but imo it is less readable.

🌐
Medium
medium.com › @lwkchan › javascripts-array-reduce-method-a-guide-and-a-commendation-on-its-flexibility-ea5dc192fad5
JavaScript’s Array reduce() method: a guide and a commendation on its flexibility | by Laura Chan | Medium
May 17, 2018 - The reduce() method then executes ... ‘Reduction’ in the context of JavaScript essentially means scaling all the multiple elements of the array into one object....
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-array-reduce-method
JavaScript Array reduce() Method - GeeksforGeeks
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.
Published   July 11, 2025
🌐
DEV Community
dev.to › sanspanic › the-javascript-reduce-method-3j8l
The JavaScript Reduce Method - DEV Community
July 23, 2021 - Above, we've told the reducer function to return the sum of the accumulator and the current value being processed. This means that as the reducer iterates through the array, each new number will be added to an ever-increasing sum held in the accumulator. Still confusing?
🌐
freeCodeCamp
forum.freecodecamp.org › guide
How to use the JavaScript Array.prototype.reduce() - JavaScript Reduce Explained with Examples
September 8, 2019 - The reduce() method applies a function against an accumulator and each value of the array (from left-to-right) to reduce it to a single value. Syntax arr.reduce(callback[, initialValue]) Parameters callback Function to execute on each value ...
🌐
Reddit
reddit.com › r/learnjavascript › learning array.reduce() method, i need some help to understand it.
r/learnjavascript on Reddit: Learning array.reduce() method, i need some help to understand it.
April 12, 2022 - Best way is to play with it. Take a look at the docs, they're easy to understand. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce
🌐
Stephan Miller
stephanmiller.com › javascript-reduce
JavaScript Reduce - A Complete Guide to the Only JS Array Function You Really Need — Stephan Miller
January 11, 2024 - The reduce method iterates over each element in the numbers array, doubles it, and adds the result to the result array. When it is done, our callback function returns the result array.
🌐
JavaScript Tutorial
javascripttutorial.net › home › javascript array methods › array.prototype.reduce()
JavaScript Array reduce() Method
November 7, 2024 - The Array reduce() method iterate over an array and reduce their elements to a single value.
🌐
Treehouse Blog
blog.teamtreehouse.com › javascript-array-methods-reduce
JavaScript Array Methods: reduce() | Treehouse Blog
September 11, 2023 - The reduce() method in JavaScript is used to apply a function to each element in an array, with the purpose of reducing the array to a single value. This method is often used for tasks such as summing the elements...
Top answer
1 of 6
2

You're very close. One thing I noticed is that you swapped the parameters to the .reduce() callback. It goes accumulator (aka total) then current value, you had it flipped. Also, the current value is the actual object of that iteration, so you can access the totalDonation property just like normal with .totalDonation. Lastly, you just need an initial value for the reduce function. This is because .reduce() takes 2 paramters - first it takes a callback function (which you have), and the second parameter is an initial value which will be used as the accumulator value for the first iteration of the loop. Since we are doing a sum, it makes sense to start with 0, hence why we pass 0 in as the initial value to reduce.

Copyconst box=document.querySelector(".wholeBox");
let contribution=[
{name:"charles",
 totalDonation:1000},
 {name:"oliver",
 totalDonation:500},
 {name:"leo",
 totalDonation:300},
];
// reduce takes 2 parameters - a callback and an initial value. We provide the callback function
// and 0 as the initial value of the accumulator so we can start from 0 when summing the totals
const totalContribution=contribution.reduce((totalValue, currValue) => {
  // currValue will be the actual object ( i.e. {name: "charles", "totalDonation": 1000} )
  return totalValue + currValue.totalDonation; 
}, 0); // <--- Here is where we provide the initial value for `.reduce()`
box.innerHTML=totalContribution;
Copy<div class="wholeBox"></div>
Run code snippetEdit code snippet Hide Results Copy to answer Expand

2 of 6
0

here's how u can do it:

Copylet contribution=[
{name:"charles",
 totalDonation:1000},
 {name:"oliver",
 totalDonation:500},
 {name:"leo",
 totalDonation:300},
];

const totalDonations = contribution.reduce((sum, curr) => {
  return curr.totalDonation + sum;
}, 0)
console.log(totalDonations)
Run code snippetEdit code snippet Hide Results Copy to answer Expand

in your attempt, you didn't reduce by the specific param that you want to sum upon

🌐
Mimo
mimo.org › glossary › javascript › reduce
JavaScript Reduce: Simplifying Array Values
The reduce function in JavaScript is a powerful array method that transforms an array into a single value.
🌐
Telerik
telerik.com › blogs › understanding-javascript-array-reduce
Understanding JavaScript’s Array Reduce
April 23, 2021 - First, we create the array that will hold our marbles, and we add them to different indexes by using array push. We call marbles.reduce, which takes a function as a first parameter. This function takes three params: the count (also called accumulator), the value, and the index.
🌐
Tabnine
tabnine.com › home › how to use array reduce method in javascript
How to Use Array reduce method in JavaScript - Tabnine
October 20, 2024 - How to Use Array reduce method in JavaScript · // // Tabnine Team / 5 minutes / October 29, 2020 · Array reduce has two very powerful features: It always returns a single value.
🌐
freeCodeCamp
freecodecamp.org › news › how-to-use-javascript-array-reduce-method
How to Use JavaScript's Array reduce() Method – Explained with Examples
November 29, 2023 - The reduce() method got its name from the functionality it provides, which is to iterate and “reduce” an array's values into one value. The easiest way to understand how the reduce() method works is through an example, so let’s see an easy one first.
🌐
CodeBurst
codeburst.io › how-to-reduce-array-to-a-single-type-11cfe2bcae66
How to Reduce Javascript Arrays With array.reduce() | codeburst
November 6, 2020 - While the reduce() method may appear confusing at first glance, the logic is simple: reduce an array to a single value by iterating through the array using a callback function. To gain more knowledge on the various ways the reduce() method can be used, check out Reduce Method In Javascript​ or MDN web doc