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

Baran Ureten
Baran Ureten
5,749 Points

empty string

why do we assign an empty string value to html?

We assign an empty value to create the variable. javascript doesn't just create new variable on the fly.

Think of it this way, you need a box to put you code/toys in. You can't turn around and just assume a box is going to show up when you have to put your toys away.

So you bring a box in before you get ready to put your toys away, this way you know you have a box available for when you ready to.

Does this make sense?

Baran Ureten
Baran Ureten
5,749 Points

I was talking about the ' html=" "; ' line on the video.

its not technically empty since it stores a space value inside.

True... it's not technically empty I guess you could say.

I see what's going on after another quick watch of the video.

They are using html as a variable. As you are a aware, the title is just used so that it's easy to understand what is in that variable.

In the function below they are appending html code after a loop to the variable.

var html = " "

loop(5 loops){

  var responseFromLoop = i-SomeDetails

// then they take that response and they write it to the global variable. // every time this loops this div will be recreated(for example, I just wrote 5 loops for easy refrenence) html += "<div id="stuff">"; html += responseFromLoop; html =+ "</div>";

}

//when this loop is completed, "html", when written to the page will have 5 different divs in side of it.

1 Answer

It is so that the variable is a string, not undefined, as it would be if you just declared it without a value:

var html;

Try changing it to what I have above and see how the document.write call treats it... :)

You also have to declare the variable with the var keyword before you can concatenate more text to it with +=, since it needs the existing value before it adds to it.

Also note, there is a difference between an empty string and a string with just a single space character inside it. Try getting the length property of each, or using it in a boolean if you want to see the results and differences.