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 AJAX Basics (retiring) jQuery and AJAX The Office Status Project Revisited

1 Answer

Steven Parker
Steven Parker
229,732 Points

They are certainly similar, but there are differences. For example, "forEach" is an array method, but "$.each" can be used on any type of collection. The arguments given to the callback are value, index, and array in "forEach" and index and value for "$.each" (key and value for objects). And of course "forEach" is a built-in, but "$.each" requires loading the jQuery library.

So based on the differences, you might choose one over the other in specific circumstances.

Dave Harker
Dave Harker
Courses Plus Student 15,510 Points

That answer is so informative I removed mine. I just wish I could mark it as 'best answer' too.

Thanks Steven,

Dave

Steven Parker
Steven Parker
229,732 Points

Thanks for the nice sentiment, but please consider leaving an answer when you "colllide" as unless it's totally redundant you never know if you may have mentioned the one unique fact someone is looking for! I actually saw your answer and you had found a performance comparison that others might find interesting. Consider putting it back.

And even when the facts match, you may have presented them in a different style that will convey them better to some students. I know for certain that not everyone "gets" my style!

Dave Harker
Dave Harker
Courses Plus Student 15,510 Points

It pretty much was ... your answer went on to explain about situational differences and about dependencies etc. It just seemed pointless leaving mine up after that :)

One thing I did link to though was something I found interesting relating to the performance of the different methods.
jQuery each vs. JS forEach just in case you're interested also. I'll keep this as a comment though, as it hardly qualifies as an 'answer'.

Kind regards,
Dave