JavaScript JavaScript and the DOM Responding to User Interaction The Event Object

Konrad Dziekonski
Konrad Dziekonski
7,797 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
8,570 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.