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

1 Answer

Steven Parker
Steven Parker
217,577 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
217,577 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