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
STAFF
Jennifer Nordell
Treehouse Staff

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 :-)