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 The Event Object

Konrad Dziekonski
Konrad Dziekonski
7,798 Points

The Event Object

hello,

I dont quite understand the meaning of the event object since whene I have changed all event instaces the code were still working, as if it was just any parameter

listDiv.addEventListener('mouseover', (x) => {
    if (x.target.tagName == 'LI') {
   x.target.textContent = x.target.textContent.toUpperCase();                        
  }});

listDiv.addEventListener('mouseout', (y) => {
  if (y.target.tagName == 'LI') {
   y.target.textContent = y.target.textContent.toLowerCase();                        
   }});

vs the event keyword

listDiv.addEventListener('mouseover', (event) => {
    if (event.target.tagName == 'LI') {
   event.target.textContent = event.target.textContent.toUpperCase();                        
  }});

listDiv.addEventListener('mouseout', (event) => {
  if (event.target.tagName == 'LI') {
   event.target.textContent = event.target.textContent.toLowerCase();                        
   }});

are there any implications of not using 'event'?

thanks!

2 Answers

Broderick Lemke
Broderick Lemke
13,483 Points

Hi Konrad!

The Event object is returned to you any time the event you specify is fired. The way we access it is by passing it to the callback like you do, and we assign it a name like event. event is a commonly used name because it makes sense to other developers, something we call a "Naming Convention". If I look at your code I know that event is likely an Event object because of the name. If I were to look at the first example where you call it x I wouldn't know if x was a string, number, or an Event object. Other common names given to the Event object are event, evt, and e. Because many people use these names if someone else looks at your code they'll have a better idea of what you're doing. It can also help yourself in a few months when you look at your own code. In summary: No matter the name you give the Event object, the underlying object is still given to you, but it's best practice to use a recognizable name.