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 One Solution

Why not always use querySelector or querySelectorAll?

Why not always use querySelector or querySelectorAll?

I'm sure you figured this out by now but querySelector(All) are slower when dealing with a ginormous amounts of information.

It is faster to compute when dealing with specific selectors. Typically it doesn't make much of a difference.

3 Answers

Dmitry Polyakov
Dmitry Polyakov
4,989 Points

It's just a matter of preference. As for me for instance I like using getElementById as much as possible.

Jamie Reardon
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Jamie Reardon
Treehouse Project Reviewer

Depends on your "needs", querySelectorAll gets all elements associated with the value you enter and groups them into an array like element, therefore you could for example, loop through them.

querySelector on the other hand, only returns the first element from the top of the DOM tree.

Tall Toad
Tall Toad
2,746 Points

Above comments makes sense to me now. I was getting the same results using querySelector/All, but Gill was using getElementsBy etc. I was confused why my code still worked. I used "for-in" loops instead of the "for" loop.