#help with const

1 messages · Page 1 of 1 (latest)

tired stratus
#

When we initialize variables with const we will not be able to reassign them. But when an html element is selected and stored in a const their innerHTML, textContent etc properties are being changed / updated. When I first saw this I thought it will throw an error. But it does not, please explain this behaviour

silk needle
#

Let's say you created a const variable named htmlelement and points to a div.

That will store the memory address (reference) of that div

If you try to change htmlelement.innerHTML you are basically changing the data inside that memory address. (Which you can!)

But you can re initialize a new member address to the html element variable.

You can learn more about memory address and reference in C language pointers.