Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Basics Making Decisions in Your Code with Conditional Statements The Conditional Challenge Solution

Kyle Pierre-Samson
Kyle Pierre-Samson
10,107 Points

Syntax & different ways to do things

I have a couple of questions:

first, whilst writing the multiple prompts to ask the questions I forgot to declare the variables as multiple "const". I actually did not declare them as anything and the code ran just fine.. I am assuming this is because it is automatically translated into a "var". Is this correct?

Secondly, What was the point of selecting the <main> and putting it into a constant at the beginning? Again I did mine differently and it worked fine, I just don't understand why Guil chose this different method which seemed odd to me. Is it better practice?

My code for reference:

document.querySelector('main').innerHTML = <h2>You got ${correct_answers} questions correct out of 5.</h2> <p>Crown earned: ${player_rank}

1 Answer

Jamie Moore
Jamie Moore
3,951 Points

Hey Kyle,

You are right in a way. But there are a few differences, this answer from stackoverflow phrases it way better than I ever could:

If you use var the variable is declared within the scope you are in (e.g. of the function). If you don't use var, the variable bubbles up through the layers of scope until it encounters a variable by the given name or the global object (window, if you are doing it in the browser), where it then attaches.

As for selecting the main element, this works either way. The benefit of storing it in a variable is that it's easier to read (subjective) and it's reusable. If the codebase grows, you can just keep using the declared variable for main rather than having to select it again every time.

Hope this helps!