What is an event in JavaScript?
An action that occurs in the browser, such as a click, key press, or form submission.
What is an event listener?
A function that waits for and responds to specific events on an element.
How do you add an event listener to an element?
Use .addEventListener('event', callback). Example: button.addEventListener('click', () => {...});
How do you remove an event listener?
Use .removeEventListener('event', callback). Example: button.removeEventListener('click', handleClick);
What is the event object?
An object automatically passed to event handlers that contains details about the event.
How do you access the element that triggered the event?
Use event.target. Example: console.log(event.target);
How do you prevent a form from submitting?
Use event.preventDefault(). Example: form.addEventListener('submit', e => e.preventDefault());
How do you stop an event from bubbling up to parent elements?
Use event.stopPropagation();
What is event bubbling?
When an event starts on the target element and moves up through its ancestors in the DOM.
What is event capturing?
The opposite of bubbling; the event moves from the top element down to the target.
What is the default event flow in JavaScript?
Bubbling (from the target element upward).
How do you handle a click event on a button?
Use button.addEventListener('click', () => {...});
How do you handle keyboard input?
Use document.addEventListener('keydown', e => {...}); or keyup. Example: if(e.key === 'Enter') {...}
How do you handle input changes in a text field?
Use input.addEventListener('input', e => {...});
How do you handle a form submission?
Use form.addEventListener('submit', e => { e.preventDefault(); ... });
How do you listen for when the mouse enters or leaves an element?
Use mouseenter and mouseleave events.
How do you detect when a user hovers over an element?
Use the mouseover event.
How do you detect when a user scrolls?
Use window.addEventListener('scroll', () => {...});
How do you listen for window resizing?
Use window.addEventListener('resize', () => {...});
How do you check which key was pressed?
Use event.key inside a keyboard event handler. Example: if (event.key === 'Enter') {...}
How do you listen for when the page finishes loading?
Use window.addEventListener('load', () => {...});
How do you listen for when the DOM is ready (before full page load)?
Use document.addEventListener('DOMContentLoaded', () => {...});
How do you attach one event listener to multiple elements?
Use querySelectorAll() and a loop. Example: buttons.forEach(btn => btn.addEventListener('click', handler));
How do you toggle a class when a button is clicked?
Use .classList.toggle('className') inside the click handler.