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

Colin Taylor
Colin Taylor
3,341 Points

I don't understand why he uses html =

I don't understand why he uses

html = to print. Is it a variable? I don't see it declared anywhere.

4 Answers

Hey Colin,

Yes, html is a variable. The way variables work in JavaScript is a little wonky. If you type out a variable that hasn't previously been initialized, is not a protected keyword, and didn't have the var keyword before it, JavaScript will automatically create the variable in the global context, regardless of where the variable was initialized, so that it can be used anywhere. By simply typing out html = and some data, you have created a global variable named html that can be used anywhere in your script, even if the html variable would have been a part of a function.

Colin Marshall
Colin Marshall
32,861 Points

Ahhh I forgot that you don't need to put var if you are setting the variable equal to something. Thanks!!!

Not necessarily. The var word is used to initialize variables but leaving it off always initializes a global scope variable. But if you use the var keyboard inside of a function it will create a local scope variable however using it outside of a function will also create a global variable like if you left it off. It really depends on whether your environment is set to strict or not as to how you can initialize variables.

Colin Marshall
Colin Marshall
32,861 Points

Yes it is a variable. It is just storing the complete string that will be outputted to the document.

I have no idea why it is not declared with var though. It is declared in the previous video, but then the declaration is gone in the video after it. Maybe Dave McFarland can help answer that?

Dave McFarland
STAFF
Dave McFarland
Treehouse Teacher

Thanks for pointing this out Colin Taylor. Yes, html is a variable, and, yes, it should have been declared earlier in the script using the var keyword. I updated the workspace for this video so that it correctly has the html variable declared.

Colin Taylor
Colin Taylor
3,341 Points

Thanks for all the help ya'll. I honestly wasn't sure and thought it was something (other than a variable) that I had missed!

Dave McFarland
Dave McFarland
Treehouse Teacher

Sorry for the confusion Colin Taylor! And yes, THANKS to everyone on the forum who steps in a helps out, and I mean you Marcus Parsons and Colin Marshall.

It's what I love to do! :)