🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEjavascript

javascript Documentation

LOADING ENGINE...

Event Listeners

AI & DATA SCIENCE // event-listeners

addEventListener attaches a handler function to an element. Options include once, capture, and passive for performance.

Syntax

el.addEventListener(type, handler, options);
// options: { once: true, capture: false, passive: true }

Deep Dive Course

The third argument to **addEventListener** accepts an **options object**: `once: true` auto-removes after first call, `capture: true` runs during capture phase (top-down), `passive: true` tells the browser you won't call preventDefault() (allows smoother scrolling). The handler receives the event object.

1Understanding Event Listeners

The third argument to addEventListener accepts an options object: once: true auto-removes after first call, capture: true runs during capture phase (top-down), passive: true tells the browser you won't call preventDefault() (allows smoother scrolling). The handler receives the event object.

💡

Use { once: true } for one-time setup events. Use { passive: true } for scroll and touch listeners to improve scrolling performance.

editor.html
const btn = document.querySelector('button');

// { once: true } - fires only once then removes itself
btn.addEventListener('click', () => {
  console.log('First click only!');
}, { once: true });

// Named function for removal
function onHover(e) { e.target.style.opacity = '0.8'; }
btn.addEventListener('mouseenter', onHover);
btn.removeEventListener('mouseenter', onHover); // clean up
localhost:3000

2Practical Example

Here is a real-world application of Event Listeners showing how it is used in production JavaScript code.

editor.html
// Passive scroll listener (improves FPS)
window.addEventListener('scroll', (e) => {
  const scrollY = window.scrollY;
  // Update scroll indicator
  updateProgressBar(scrollY);
}, { passive: true }); // tells browser: no preventDefault inside
localhost:3000

3Best Practices

Follow these guidelines when working with Event Listeners:

1. Store handler references for cleanup with removeEventListener

2. Use { once: true } for one-time handlers

3. Use { passive: true } for scroll/touch for better performance

⚠️

Tip: Use { once: true } for one-time setup events. Use { passive: true } for scroll and touch listeners to improve scrolling performance.

editor.html
const btn = document.querySelector('button');

// { once: true } - fires only once then removes itself
btn.addEventListener('click', () => {
  console.log('First click only!');
}, { once: true });

// Named function for removal
function onHover(e) { e.target.style.opacity = '0.8'; }
btn.addEventListener('mouseenter', onHover);
btn.removeEventListener('mouseenter', onHover); // clean up
localhost:3000

Examples

Example 01Basic Usage
const btn = document.querySelector('button');

// { once: true } - fires only once then removes itself
btn.addEventListener('click', () => {
  console.log('First click only!');
}, { once: true });

// Named function for removal
function onHover(e) { e.target.style.opacity = '0.8'; }
btn.addEventListener('mouseenter', onHover);
btn.removeEventListener('mouseenter', onHover); // clean up
Example 02Advanced Example
// Passive scroll listener (improves FPS)
window.addEventListener('scroll', (e) => {
  const scrollY = window.scrollY;
  // Update scroll indicator
  updateProgressBar(scrollY);
}, { passive: true }); // tells browser: no preventDefault inside

Best Practices

  • Store handler references for cleanup with removeEventListener
  • Use { once: true } for one-time handlers
  • Use { passive: true } for scroll/touch for better performance

Interview Question

Why should you use { passive: true } for scroll listeners?

Hint: The browser waits before scrolling without it.

Browsers must wait for scroll listeners to complete before scrolling, in case they call preventDefault(). { passive: true } tells the browser upfront that the listener won't call preventDefault(), so it can scroll immediately without waiting. This significantly improves scroll performance on mobile.

Exercises

MediumPractice using Event Listeners in a real scenario.
View Solution
const btn = document.querySelector('button');

// { once: true } - fires only once then removes itself
btn.addEventListener('click', () => {
  console.log('First click only!');
}, { once: true });

// Named function for removal
function onHover(e) { e.target.style.opacity = '0.8'; }
btn.addEventListener('mouseenter', onHover);
btn.removeEventListener('mouseenter', onHover); // clean up

Frequently Asked Questions

Why should you use { passive: true } for scroll listeners?

Browsers must wait for scroll listeners to complete before scrolling, in case they call preventDefault(). { passive: true } tells the browser upfront that the listener won't call preventDefault(), so it can scroll immediately without waiting. This significantly improves scroll performance on mobile.

Related Functions

EventsSelection-MethodsElements-Manipulation