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 Tracking Multiple Items with Arrays Build a Quiz Challenge, Part 2 Solution

Keith Barlow
Keith Barlow
5,114 Points

html variable

Why don't I have to declare the variable html ?

All the other variables need var varName; before they hold data.

At the end of the code we just write html = "You got " + correctAnswers + " question(s) right."

And this variable is used as a parameter for the pint() function..

Is html a 'built in' variable?

Nathan Phan
Nathan Phan
9,898 Points

I just tried something in the console log...

x=1;
alert(x);

and it worked.

Turned out you didn't have to declare a variable in js. JS will just automatically set it up and assign the scope of that variable to global if the var hasn't been declared somewhere else before.

Very good point well made. I was searching for this question before I posted. I've put the search in Google and there are a lot of solutions on Stack Overflow. Do I understand them all? No.

However, It seems that it is something that you can get away with if there are no other instances of it declared in functions or globally but with a lot of code and a common name like 'html', it could be used again and it might return different values.

3 Answers

Nathan Phan
Nathan Phan
9,898 Points

var html was declared in part 1 video. In this case it's just a normal string.

the HTML variable is set to an empty string like so:

var HTML = " ";

this is used to apply a string to the variable later in the code.

Keith Barlow
Keith Barlow
5,114 Points

Thanks for that..

BUT I am only working with one page of JS and at no pint on that page was the html variable declared.

I understand it may have been declared in a previous video BUT its not on the page I am working with... I am very confuses..

What do you mean page? are you working in workspaces, or your own text editor?

I see Keith's point. "html" is not declared as a variable anywhere in this workspace. It doesn't matter if a workspace previously declared it, since each workspace compiles independently of other workspaces.

So did Dave make a mistake, or is this common practice to use a variable without declaring it?

Nathan Phan
Nathan Phan
9,898 Points

I added another comment shortly after that post. Turned out you didn't need to declare variables in javascript for them to work. It's a much better practice to do so tho.