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 forEach in JavaScript Practice forEach Challenge 2 Solution

Why do we use push() method?

I understand what push() method does but I need some explanation on why do we use push() method in dayAbbreviations.array?

1 Answer

Hi Julia!

You are building a new array and the push method is allowing you to add one new abbreviated element to the dayAbbreviations array for each iteration of the forEach loop.

You could also get the same result using the ES6 map function like this:

let dayAbbreviations = days.map( day => day.slice(0, 2) );

More info:

https://www.w3schools.com/jsref/jsref_obj_array.asp

I hope that helps.

Stay safe and happy coding!

Thank you