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, Arrays and Objects Simplify Repetitive Tasks with Loops The Refactor Challenge Solution

Vithu Mathi
seal-mask
.a{fill-rule:evenodd;}techdegree
Vithu Mathi
Front End Web Development Techdegree Student 3,474 Points

what is the var html = " " variable for?

Can somebody help me with the purpose of the var html = " " statement? I dont quiet understand what its doing in this script.

2 Answers

Matthew Tran
Matthew Tran
16,343 Points

To use his analogy, it starts the box to hold the HTML components. You're producing HTML components with your JavaScript but do not have a place to put it so it gets spit out when it is needed. You assign the variable HTML to equal " " to start an empty box. After that, you start adding to it.

james south
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
james south
Front End Web Development Techdegree Graduate 33,271 Points

the html variable holds the string that is written to the page using document.write at the end of the script. the string starts out empty but is added to with each iteration of the loop. it holds html code that the browser interprets to add the elements to the page.