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 Listening for Events with addEventListener()

2,483 Points

Why do you need to assign listItems.textContent to itself?

In this example:

listItems.addEventListener('mouseover', () => { listItems.textContent = listItems.textContent.toUpperCase(); }); Why do we have to assign listItems.textContent.toUpperCase(); to listItems.textContent? I know it doesn't work if we don't but I want to understand WHY is it that i cannot just the following instead:

listItems.addEventListener('mouseover', () => { listItems.textContent.toUpperCase(); });

2 Answers

O V The reasoning behind this is sort of in your question. When you call listItems[0].textContent.toUpperCase(); you are grabbing a value (which in this instance is the uppercase version of the text content inside of the first instance of your list items) but you are not actually setting(or assigning) that value to anything. Putting listItems[0].textContent on the left side of the operator actually sets(assigns) that uppercase value to the text content of the first instance of a list item. To attempt to simplify this explanation, imagine you want to set the text content to the word "hello". You couldn't just put

listItems[0].addEventListener('mouseover', () => { "hello" });

instead you would have to put:

listItems[0].addEventListener('mouseover', () => { listItems[0].textContent = "hello"; });

I hope this helps!

From MDN

The toUpperCase() method returns the value of the string converted to uppercase. This method does not affect the value of the string itself since JavaScript strings are immutable.