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

Event Listeners: Parameter for Event Object in Handler Functions

There's a bit of confusion for me around how the browser knows to insert the event object into the event parameter within a handler function if that parameter may be named whatever you'd like (other than event, evt, or e).

For example: If I were to choose to name the parameter (for the event-object argument) as banana, but also had other unique parameters for the handler function, then how would it know which parameter was meant to receive the event-object argument?

It's definitely useful to have something like e.target to single out whatever element has just been interacted with, but the parameter part is very much confusing me.

1 Answer

I'm led to believe the event object can be named anything you want (although e or event are logical and probably best-practice) and that the first argument will always be the event object (in the addListener callback function).

More info:

https://stackoverflow.com/questions/35936365/what-exactly-is-the-parameter-e-event-and-why-pass-it-to-javascript-functions

I hope that helps.

Stay safe and happy coding!