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 trialIliya Narsiya
3,387 PointsUsing 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
Alexandria Wagner
7,038 PointsHi 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
Iliya Narsiya
3,387 PointsIliya Narsiya
3,387 PointsI mean why in this code, we assign variable html. On the other variable the code is not running.