Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Array Iteration Methods Combining Array Methods Combining filter() and reduce()

Swan The Human
seal-mask
.a{fill-rule:evenodd;}techdegree
Swan The Human
Full Stack JavaScript Techdegree Student 19,338 Points

::Just for clarification:: If you do not set an initializer in the reduce() method, does it default to being an object?

I think I understood that correctly towards the end of the video. Joel explains that he is using 0 as an initializer (in the second portion of the video), so that the product.price knows to add it to a number. In the first portion of the video, where we created a function to return the highest priced product under $10, we did not use an initializer and I wasn't sure why. Just confirming that the default initializer will always be an object unless specified otherwise

1 Answer

Steven Parker
Steven Parker
231,269 Points

An implicit initializer is not necessarily an object. When no initializer is provided, the first value of the array becomes the initial value, and the process begins with the second one. So it's based on what the array contains, which in this example just happens to be an object.

For more details, see the MDN page on reduce().