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 trialColin Taylor
3,341 PointsI 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
Marcus Parsons
15,719 PointsHey 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
32,861 PointsYes 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
Treehouse TeacherThanks 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 Marshall
32,861 PointsThanks Dave!
Colin Taylor
3,341 PointsThanks 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
Treehouse TeacherSorry 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.
Marcus Parsons
15,719 PointsIt's what I love to do! :)
Colin Marshall
32,861 PointsColin Marshall
32,861 PointsAhhh I forgot that you don't need to put var if you are setting the variable equal to something. Thanks!!!
Marcus Parsons
15,719 PointsMarcus Parsons
15,719 PointsNot 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.