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

Sara Masek
seal-mask
.a{fill-rule:evenodd;}techdegree
Sara Masek
Front End Web Development Techdegree Student 11,218 Points

Why does accessing the document's HTML come last in this script?

Hello, I was wondering why it's necessary to put the line "document.querySelector('main').innerHTML = html; " at the very end of the script, instead of declaring it with the other variable up top, which is what I initially tried to do when refactoring. When I removed it from the bottom and move it to the top, everything disappears after refreshing. I was just wondering why this is, since I'm trying to understand every piece that goes into making this program run. Thank you to anyone who is able to shed some light on this.

2 Answers

Reggie Williams
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Reggie Williams
Treehouse Teacher

Hey Sara Masek ! Javascript is evaluated from top to bottom. Because of this, when you set the innerHtml with the statement you mentioned before the html definition is complete the innerHTML will be set to a value of ' ' which is the original value of html