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 DOM Scripting By Example Adding and Removing Names Registering Names

Shouldn't li have been 'let' as opposed to 'const' because we altered one of its properties (textContent)?

.

2 Answers

Louise St. Germain
Louise St. Germain
19,424 Points

Hi Amandeep,

Const is OK because it contains a reference to an HTMLElement object (in this case, an li list item), not to all the individual properties of the li. Even if properties inside the object change (which is what you're referring to), it's still pointing to that li object as a whole. Think of it as pointing to the address of a house on a street. People can come in and out of the house and you might remodel a room inside or paint the kitchen, but the house's street address is still the same.

If we suddenly wanted to take that same const variable and assign it to a totally different object, like the header image or something, then yes, JavaScript would give you problems. (Similar to if you suddenly wanted it to point to a different physical house address down the street.) But as long as the const variable remains pointing to that original li, it will be fine.

I hope this helps!

I love the analogy :)