Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

Swan The Human
Swan The Human
Full Stack JavaScript Techdegree Student 19,326 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
217,627 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().