Function References and Browser Event Listener Removal

A function reference is a value identifying a particular JavaScript function object; passing it does not invoke the function. For browser event cleanup, retain and reuse the same callback reference so `removeEventListener` can match the registration.

In JavaScript, functions are objects, and a function reference is a value that identifies one specific function object. You can store it in a variable, pass it as an argument, or return it from another function. Passing a function reference does not call the function; calling it requires parentheses, such as `handleClick()`.

This distinction matters with browser events. `addEventListener` registers a callback, while `removeEventListener` needs the matching callback reference to remove that registration. Defining a new function with identical code does not provide the same reference: function identity, not source-text similarity, is what matters.

For reliable cleanup, keep the reference for as long as the listener may need to be removed. The event type and relevant listener options must also match the registration; in particular, the capture setting is significant. Recreating an equivalent inline arrow during removal will not match the originally registered arrow.

References are also useful for sharing callbacks across elements and for passing handlers to APIs. Be aware that a handler created inside a component or setup function may be recreated on each run; store or memoize it when stable identity is required by the surrounding lifecycle.

Key points

Interview questions

What is a function reference, and how does it differ from calling a function?

A function reference is a value identifying a particular function object. Writing `handler` passes that value, while `handler()` invokes it and passes the result. Browser listener removal depends on the original callback identity, so an equivalent newly created function is not interchangeable.

Why does removing a listener with a new inline arrow function not work?

The registration stores the callback object supplied at that time. Each arrow-function expression evaluation creates a function object, so the arrow used in removal is not the same object as the one registered. Save the arrow in a variable and use that same variable for both operations.

How would you manage event-listener callback identity in a component lifecycle?

Create the callback once in the scope that owns the listener lifecycle, then use that same reference for registration and cleanup. Ensure cleanup uses the same event type and capture behavior; other option differences do not generally distinguish registrations for removal. In component-based code, avoid assuming a callback recreated on every render has stable identity unless the framework or code explicitly preserves it.

Are anonymous callbacks unusable with event listeners?

An anonymous function can still be passed as a listener, but if no reference is retained it is difficult to remove that specific registration later. A named function declaration or a variable holding the callback makes the reference available for cleanup. Anonymous callbacks are reasonable when their lifetime is intentionally tied to the target or another cleanup mechanism is used.