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 Practice Object Interaction Checking Out and Returning a Book Solution: Charging Fines to Patrons

Seth Missiaen
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Seth Missiaen
Web Development Techdegree Graduate 21,652 Points

Is the content inside "this.patrons.filter()" an arrow function?

I'm confused about this code block:

    const latePatrons = this.patrons.filter(patron => 
        (patron.currentBook !== null && patron.currentBook.dueDate < now)
    );

Is the => indicating an arrow function? Why is that necessary? I understand the logic inside the next set of parantheses (currentBook is not null and dueDate is less than current day). Maybe I'm missing how filter() works but couldn't you use just the content inside the second set of parentheses like this?

const owing = this.patrons.filter(patron.currentBook !== null && patron.currentBook.dueDate < now);

Thanks so much!

1 Answer

Steven Parker
Steven Parker
231,275 Points

The .filter method doesn't take just a boolean value as an argument, it requires a reference to a callback function that will return a boolean value when invoked. The arrow function expression provides a concise way to define an anonymous function as the body of the argument.

For more details, see the MDN page on .filter.