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 One Solution

Const in DOM

Hello everyone!

I have a little question. So, my code is working properly however I don’t understand one thing. Why is it possible to modify / change content which we store in ‘const’?

Example: const deleteButton = document.createElement('button'); deleteButton.textContent = 'Delete';

Was it mentioned somewhere in the DOM tutorials? I understand that we can also use ‘let / var’ instead of ‘const’ but shouldn’t only ‘let / var’ work in this case?

Essentially, the constant means a memory address. Think of it like your phone number. Your phone number is an address of your phone's location... when you call that phone... the person answering it can change... but the phone number itself is constant.

Street addresses work the same way. If you list your address somewhere you can get mail sent to that location... but the properties of that house can change. You can even make that address NULL by demolishing the house.... but the address still exists. It's constant.

When you use const, you can't change the address... so someone can't pull a fast one and point your mail somewhere else... they can't suddenly say that 123 Main Street, 90210 is in a different city and steal your amazon packages.

Using LET means you have a forwarding address or a PO Box that can be forwarded wherever you want it.

1 Answer

Well, that's a little thing about javacript.

When you make a const, the constant itself is constant, but the properties of the constant aren't.

So while javascript will throw an error if you change deleteButton, since that's a const, it will be completely fine with you changing the property deleteButton.textContent

terry okey
terry okey
3,187 Points

Oh wow thanks. I've been wondering that for weeks.