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

Why wait for the jQuery part to use .each(), when plain Javascript also has forEach()?

Writing a for loop is so much more abstract than forEach.

1 Answer

This is most likely due to the dreaded Internet Explorer 8. The pure JavaScript .forEach() method was not introduced to IE until version 9, so for backwards compatibility it would be better to rely on jQuery.

The other argument I have found is that in some performance tests, $.each() actually seems to perform better than Array.forEach(), though I have read sources that claim this is no longer an issue. I will say that when I run the following performance test on MY machine, using Chrome/OS X, .each seems slightly faster.

forEach vs. jQuery .each vs. for vs for..in

Thanks!