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

Daniel Richardson
Daniel Richardson
3,148 Points

Why the empty string in var html?

Hi I don't fully understand the purpose of the empty string in the html variable

I've tested removing = ''; from var html the script still runs but the page shows "undefined" at the start of the number list.

Can someone explain to me why this happens?

Many Thanks Dan

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! Yes, when you say var html; this creates a variable named html but at this point the value is undefined. Later, when you try concatenating on to it it takes undefined and then concatenates on to the end. This is why the undefined is showing up. However, if you start by declaring the var html = ''; then the original value is not undefinedd, but rather an empty string and you can concatenate onto it as much as you like :smiley:

Hope this helps! :sparkles:

Daniel Richardson
Daniel Richardson
3,148 Points

Got it! Thanks Jennifer perfect explanation :-)