Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is e in JavaScript

Understanding JavaScript's Mysterious 'e'

If you have been learning JavaScript, you might have come across a letter 'e' popping up in some of your code snippets, especially within event handlers. This 'e' is often passed as an argument in a function. For beginners, seeing this letter can be quite puzzling.

So, what is this 'e' in JavaScript? Simply put, 'e' stands for event object. It's a convention to use 'e', but you can name it whatever you want.

Let's have a look at an example:

button.addEventListener('click', function(e) {
  console.log(e);
});

In the code above, we have an event listener attached to a button. When the button is clicked, it triggers a function, and that function receives an event object (that's our 'e').

The Event Object

The event object contains all the information about the event that just happened. For example, if it's a click event, the event object will contain details about where the click happened, what was clicked, the time it happened, and so on.

Think of it like a detective arriving at a scene - the event object is the summary report the detective compiles with all the details about what just transpired.

Let's expand the previous example:

button.addEventListener('click', function(e) {
  console.log('Event object:', e);
  console.log('Event type:', e.type);
  console.log('Clicked element:', e.target);
});

Here, we're logging the entire event object and also some specific properties of the event object. The 'type' property gives us the type of the event (in this case, 'click') and the 'target' property provides the HTML element that was clicked.

Why Do We Need 'e'?

Having access to the event object is really useful in JavaScript. It allows us to control how our application responds to user interactions. Imagine if you're creating a game, you'd want to know details like which key was pressed or where the mouse was clicked to control game elements.

For instance, consider this piece of code:

window.addEventListener('keydown', function(e) {
  if(e.key === 'ArrowRight') {
    console.log('Right arrow key was pressed!');
  }
});

In the above code, we're listening for a 'keydown' event on the entire window. When a key is pressed, our function checks if it was the 'ArrowRight' key by using e.key. If it was, we log a message.

Preventing Default Behavior

The event object also allows us to prevent the default behavior of certain events. This is done using the preventDefault method.

Let's say we have a form and we want to process the form data using JavaScript rather than sending it to a server. By default, forms are submitted to a server, causing the page to refresh. We can prevent this with e.preventDefault().

Here's an example:

form.addEventListener('submit', function(e) {
  e.preventDefault();
  // Process form data here
});

In this case, calling e.preventDefault() stops the form from being submitted in the traditional way, allowing us to handle the form data in our own custom function.

Wrapping Up

After journeying through JavaScript's event object, 'e' should no longer be a mystery. It's just a summary report of an event that has happened, carrying all the juicy details about the event. It's like our JavaScript detective, always ready to jump in and tell us what went down.

Remember, 'e' is not mandatory, it's merely a convention. You could use 'event', 'evt', 'banana', or any name you like. What's important is understanding its purpose and how to use it in your code.

As a final thought, the power of JavaScript's event handling lies in the event object and its properties. By harnessing this power, you can create dynamic and interactive web applications that respond intelligently to user interactions. Happy coding!