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 Asynchronous Programming with JavaScript Asynchronous JavaScript with Callbacks Implement a Callback

Understanding the use of the map method in this video

I see that Guil has used the map method to iterate through the functions he has created. But I have to question why he used it. Since we used .map, and it creates a whole new array, what exactly are we going to do with this "array". We created an array that is not being accessed in other parts. I can see how this calls for less lines of code but I'm just curious to see what the left out array would have use for as opposed to a regular loop that is iterating throughout the objects array length.

1 Answer

Dane Parchment
Dane Parchment
Treehouse Moderator 10,631 Points

You are correct, as an example of map, this is unfortunately a very poor use-case and actually demonstrates an anti-pattern. For...of or forEach were better alternatives for this. Now at the end of the day nothing is actually going to happen because of this, garbage collection will take care of that unused array, it's just not a good practice to do so.