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 JavaScript and the DOM (Retiring) Responding to User Interaction Adding an Event Listener

ian izaguirre
ian izaguirre
3,220 Points

Question about Challenge answer and using const ?

This is the challenge answer:

const warning = document.getElementById("warning");
let button;

button = document.getElementById('makeItRed');

button.addEventListener( 'click', () => {
  warning.style.backgroundColor = 'red';
});

I want to know:

const warning = document.getElementById("warning"); // we used const
let button;

button = document.getElementById('makeItRed');

button.addEventListener( 'click', () => {
  warning.style.backgroundColor = 'red';  // Why is there no error here?
});

"const" does not allow a variable to be redefined, yet this give no error, Why ? For example, the code below will give an error. I would think the above code would only work if we wrote let warning = document.getElementById("warning"); instead of using const warning. I still do not see how this event handler can give no error if it seems to be breaking the rule for "const", since it assigns const a new value ?

This gives Error , since it would only work if we use "Let" :

const dog = 'barking';
dog = dog.style.color = 'red';
console.log(dog);

1 Answer

andren
andren
28,558 Points

With a const variable you cannot change what object it is assigned to, but the object itself can change. Take an array for example:

const names = ["James"];
names.push("Mary");
console.log(names);

In the above code I assign a list to the constant names and then add a name to that list, that is completely valid code because I have not assigned a different list to the constant, only changed what is present in said list.

If I were to do this however:

const names = ["James"];
names = ["James", "Mary"]
console.log(names);

Then that would not work, because in that case I actually am assigning a new list to the constant, rather than just changing the one that is already assigned to it.

The code in your example is the same, you don't assign a different HTML object to the constant, you just change a property of the object. In your second example you are trying to actually change what object is stored in the constant which is invalid, strings also don't have a style property so that example wouldn't work regardless but that is somewhat beyond the point.

ian izaguirre
ian izaguirre
3,220 Points

I really appreciate you taking the time explaining this in great detail, it cleared it up for me. Thank You :-)