JavaScript DOM Scripting By Example Adding and Removing Names Registering Names

Amandeep Pasricha
Amandeep Pasricha
14,929 Points

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

As asked.

1 Answer

Louise St. Germain
Louise St. Germain
19,397 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!