Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Reduce an Array to One Value

In this lesson, you added up Rosa’s ratings again, this time with .reduce() instead of a loop. You saw what goes wrong when you leave out the starting value, traced the fixed call one call at a time, and watched each return become the next call’s accumulator.

Key Points

  • The loop version starts a total at 0, adds each book’s rating, and logs 14 for Rosa’s three books: Parable of the Sower (5), Little Fires Everywhere (4) and Project Hail Mary (5).
  • Instead of a let that changes inside a loop, .reduce() hands back the total as one value, ready to store in a const, the way .map() hands back a new array.
  • Like .map(), .reduce() takes a function. It calls that function for the items, one after another, and passes it the running total first, then the current item, plus two extras you can ignore for now. Whatever the function returns becomes the running total for the next call.
  • Watch out for a call with no starting value. rosa.books.reduce((total, book) => total + book.rating) logs [object Object]45, not 14, and no error warns you. With no starting value, .reduce() takes the first book itself as the starting total and calls the function from the second book on.
  • Traced call by call, the first call adds 4 to Rosa’s first book, the whole object. When you add a number to a plain object like Rosa’s first book, JavaScript turns it into the text [object Object] and joins the number on as text, so the call returns "[object Object]4". That text is the total for the second call, and the 5 joins on too.
  • On an empty list, such as Luis’s books, the same call has no first item to start from, so it stops with TypeError: Reduce of empty array with no initial value. The initial value is the starting value you left out.
  • The fix is a second argument after the function: the starting value, 0. Traced again, the first call gets 0 and returns 5, the second gets 5 and returns 9, and the third gets 9 and returns 14. When no items are left, .reduce() gives back the last return.
  • The running total is called the accumulator. Each call’s return becomes the next call’s accumulator.
  • So your function has to return it. A one-line arrow function returns its expression for you. Written with braces and no return, as in rosa.books.reduce((total, book) => { total + book.rating; }, 0), each call returns undefined, the next call’s accumulator is undefined too, and so is the result.
  • With a starting value of 0, Luis’s call logs 0. With no books, the function never runs, so .reduce() gives back the starting value.
  • The rule: always pass the starting value. It makes the first call start from the right kind of value, and it gives an empty list a result instead of an error.

Final Code

The script report.js holds the club’s full log. Below it, Rosa’s and Luis’s totals each come from .reduce() with a starting value of 0. Copy it to follow along with the video:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 Earlier, you added up each member’s ratings with a loop. Here’s that loop for one member, Rosa. The total starts at zero, each book adds its rating, and the script logs fourteen.

00:12 In this lesson, you’ll get the same fourteen from .reduce(), an array method. It reduces a whole list to one value. Instead of a let that changes inside a loop, it hands back the total, ready to store in a const.

00:27 Like .map(), the .reduce() method takes a function. It calls that function for the books, one after another. Each time, it passes the running total first, then the current book, plus two extras you can ignore for now.

00:41 Whatever the function returns becomes the running total for the next call. So the function’s body can be the loop’s body: the total plus the book’s rating.

00:51 Run it, and you’d expect fourteen. You get this instead: [object Object], then a four, then a five. That’s text, not a number, and no error warned you.

01:06 Compare it with the loop. The loop set the total to zero before the first book. This call never says where the total starts. So .reduce() takes the first book itself as the starting total, and calls the function from the second book on.

01:22 Trace it call by call. In the first call, the total is Rosa’s first book, the whole object, and the book is Little Fires Everywhere. When you add a number to a plain object like this book, JavaScript turns it into the text, [object Object], and joins the four on as text.

01:42 That text is the total for the second call, and the five joins on too.

01:48 Now try the same call on Luis, who has no books. Earlier, your loop gave him zero points. Here, there’s no first book to start from, so JavaScript stops with a TypeError and prints no total.

02:01 It says, “Reduce of empty array with no initial value.” The initial value is the starting total you left out.

02:11 The fix is a second argument, after the function: the starting total, zero. Trace it again. The first call gets zero and Parable of the Sower, and returns five.

02:23 That five becomes the total for the second call, which adds Little Fires Everywhere’s four and returns nine. The third call adds Project Hail Mary’s five and returns fourteen.

02:34 No books are left, so .reduce() gives back the last return, and the script logs fourteen.

02:42 This running total is called the accumulator. Each call’s return becomes the next call’s accumulator.

02:49 So your function has to return it. The one-line arrow returns its expression for you. Write the body in braces with no return, and each call returns undefined.

02:59 The next call’s accumulator is undefined too, and so is the result.

03:05 Back to Luis, with a starting zero this time. Your turn: pause the video and predict what the script logs for him.

03:18 It logs zero. With no books, the function never runs, so .reduce() gives back the starting value.

03:27 Here’s the rule: always pass the starting value. It makes the first call start from the right kind of value, and it gives an empty list a result instead of an error.

03:39 Rosa’s three books are a toy list, but the same call adds up a list of any length. Next, in Build Counts With .reduce(), you’ll start from an empty object instead of zero, and count the club’s books by genre.

Become a Member to join the conversation.