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

Sara Masek
seal-mask
.a{fill-rule:evenodd;}techdegree
Sara Masek
Front End Web Development Techdegree Student 11,513 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
STAFF
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