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

Philip Schultz
Philip Schultz
11,437 Points

Why isn't the html variable declared?

When we are displaying the text to the browser through the variable 'html', why don't we have to declare it (var html)?

1 Answer

Chris Shaw
Chris Shaw
26,676 Points

Hi Phillip,

I believe this is an oversight on the teachers' behalf, while it's recommended that you prefix variables with the var keyword, it isn't required in non-strict mode which allows you to declare a variable in the global scope without using var. Nowadays, this is considered to be bad practice as it can create conflicts if you aren't using a closure to seal your code off from the world.

That said, if we take the same code from the video and add 'use strict'; to the top of the file the browser will complain that we have declared a variable without first declaring the var keyword.

In general, you should always prefix variables using var to prevent scoping issues.

Hope that helps.

H Yang
H Yang
2,066 Points

I wondered this as well. Thanks for the explanation!