0

We know if we declare a variable as const we can't reassign anything to them. Then how do you justify altering certain html or css properties of a node/nodelist selected in Javascript?

<h2 class="targetHTML">Dynamically varying content A</h2>

javascript part

const nodeHTML = document.querySelector(' .targetHTML');
nodeHTML.textContent = 'Varied content B';

This changes the text which is displayed within the H2 tag. The node and whatever properties or attributes of it once stored in the const should freeze?

code-mon
  • 71
  • 2
  • 10
  • `nodeHtml` is a reference type, not a value type. `const` is referring to the reference, not the node. – Robert Harvey Oct 17 '20 at 19:49
  • 2
    This question has been answered before (with explicit reference to DOM manipulation): https://stackoverflow.com/questions/42833540/keyword-const-does-not-make-the-value-immutable-what-does-it-mean – Pida Oct 17 '20 at 19:51

2 Answers2

0

The const is related to what object the variable points to. The variable cannot change what object is assigned to it, but the object itself can still have its properties changed.

Luke B
  • 2,075
  • 2
  • 18
  • 26
0

const is CONSTANT by reference, not by value.

karlmarxlopez
  • 1,019
  • 1
  • 8
  • 16