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

Is the addEventListener considered a function?

I've heard of it being referred to as a method, but it seems to me that it works like a function. Is it a function or method? Can someone explain the difference?

Thanks!

1 Answer

Hi Nicholas,

Simply put - methods are functions that are associated with an object. Therefore all methods are functions. And, just to get silly - since functions in javascript are associated with the global object they are actually methods. Generally in conversation - methods are functions defined as part of the object/class and functions are defined by themselves.

According to the MDN addEventListener doc, it is a method

The EventTarget method addEventListener() sets up a function that will be called whenever the specified event is delivered to the target. Common targets are Element, Document, and Window, but the target may be any object that supports events (such as XMLHttpRequest).

Hope that helps. :tropical_drink: :palm_tree: