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 For Loops

Using the html

Why is an empty string, we can only use html? Why other variables code does not work?

var html = ' ';

for (var i = 1; i <= 10; i++){ html += '<div>' + i + '</div>';

} document.write(html);

1 Answer

Hi Iliya,

When you declare a variable, you can name that variable any name, within standard naming conventions. You can find more information about declaring variables on the Mozilla Developer Network page on JavaScript grammar.

When using concatenation, it's important to be careful about separating the pieces you are concatenating. The example code from the lesson separates the string values from the variable being affected in the for loop. Your example lists {html += " + i + ";}, as compared to the code below:

var html = '';
for(var i = 1; i <= 10; i+=1){
  html += '<div>' + i + '</div>';
}

I hope this helps! Best, Alex

I mean why in this code, we assign variable html. On the other variable the code is not running.