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 Loops Working with 'for' Loops The Refactor Challenge – Duplicate Code

The Document.QuerySelector

I was curious about the document.querySelector('main').innerHTML = HTML; I noticed the instructor kept this code outside the for loop. But I noticed when i put it inside it still work as expected. It preferred practice to keep cleaner code that displays the information outside the loop or is this just preference? Also Is there a course on cleaner code best practices.

1 Answer

Steven Parker
Steven Parker
231,261 Points

The preferred practice is to keep anything that only needs to be done one time out of a loop. Technically, it still works if you do it over and over, it's just not necessary and it wastes computer resources.

I'm not aware of a separate course on best practices and optimization, but you'll find tips on both scattered throughout the courses and code examples.