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

2 Answers

Steven Parker
Steven Parker
212,126 Points

It's possible and quite common. After your statement, the variable "main" will be a reference to the <main> element of the document.

karan karan
seal-mask
.a{fill-rule:evenodd;}techdegree
karan karan
Web Development Techdegree Student 7,016 Points

Hi Steven, thanks again for the reply, as it holds the reference but it does not hold the actual content and as we know variable's value can be changed, so how the code will be interpreted in the browser, but i thought it will be stored in the variable

Steven Parker
Steven Parker
212,126 Points

If you wanted to get the content instead of the reference, you could assign this instead:

const main = document.querySelector('main').innerHTML;

Just be aware if you do that, the variable will just hold the content as a string and can't be used as a reference to the element. So this wouldn't serve the same purpose as what the variable main is being used for in the video.

karan karan
seal-mask
.a{fill-rule:evenodd;}techdegree
karan karan
Web Development Techdegree Student 7,016 Points

Okay so you mean to say with .innerHTML the variable will hold the values in it instead of the reference, as .innerHTML is used to take over the content part right, so by just giving the reference of the Structure which is here-

( const main = document.querySelector('main') )

by giving

const main = document.querySelector('main').innerHTML;

we are giving it the inner values instead? but doesn't that is the same as we just want to access the data inside even if we give the inner values if we change the value of the variable the data will be replaced?

Steven Parker
Steven Parker
212,126 Points

If you want to use the variable to access anything later, you must assign it as in your original code example. If you want the element's HTML content, you can use the example I gave.

If you change the variable later, it doesn't matter whether it is being used for content or reference, whatever it was holding gets replaced by the new assignment.