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

Javascript forEach seems simpler...

Using the native JavaScript forEach seems simpler than jQuery's each, because we don't need to specify the index. What do you guys think?

Example:

response.forEach(function (employee) {
  if (employee.inoffice === true) {
    statusHtml += '<li class="in">';
  } else {
    statusHtml += '<li class="out">';
  }
  statusHtml += employee.name + '</li>';
});
Clayton Perszyk
Clayton Perszyk
Treehouse Moderator 49,057 Points

Unless the course was aiming to teach jQuery, I would agree that forEach is better idea, especially if you don't need jQuery anywhere else in the code.

1 Answer

Steven Parker
Steven Parker
243,658 Points

The choice would be based on what you were applying it to. You would use ".forEach" only on arrays, ".each" only on jQuery objects, and "$.each" on both arrays and objects.

So only for arrays do you have an optionbetween two of them, and only if you have jQuery loaded.

And in ".forEach" the index argument is optional, and in ".each" and "$.each" it can simply be ignored.