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 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
211,016 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.