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 trialKeith Barlow
5,114 Pointshtml 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?
Joseph Lander
Full Stack JavaScript Techdegree Graduate 27,765 PointsVery 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
9,898 Pointsvar html was declared in part 1 video. In this case it's just a normal string.
Jacob Mishkin
23,118 Pointsthe 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
5,114 PointsThanks 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..
Jacob Mishkin
23,118 PointsWhat do you mean page? are you working in workspaces, or your own text editor?
macmillandesign
Courses Plus Student 8,424 PointsI 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
9,898 PointsI 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.
Nathan Phan
9,898 PointsNathan Phan
9,898 PointsI just tried something in the console log...
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.