Event Handlers — JS Technical Interview Questions & Answers

onclick attributes vs addEventListener().

Event Handlers

  • Event Handlers (js) is a JS topic. In plain words you use it for a short Event Handlers js example in a script in the browser. Don’t start with a slogan — start with that picture.
  • Smallest example: the smallest Event Handlers js example you can type. Type it, run it, and say what you see. If you can do that from memory, you know Event Handlers (js).
  • From the example next to this theory: onclick attributes vs addEventListener().
  • Trap: only saying “Event Handlers js” with no example. Fix that before you talk about advanced JS.
  • Viva: what is Event Handlers (js)? Then show the smallest Event Handlers js example you can type. Then name the trap.

Technical Interview Tip: What is Event Handlers (js)? Show this: the smallest Event Handlers js example you can type. Trap: only saying “Event Handlers js” with no example.

Code Examples for Event Handlers

Event Handlers — working example

// Browser delegation sketch:
// list.addEventListener("click", (e) => {
//   if (e.target.matches("button.del")) {
//     e.preventDefault();
//     e.target.closest("li")?.remove();
//   }
// });

// Runnable stand-in:
function onClick(e) {
  if (e.target === "del") {
    e.preventDefault?.();
    return "removed";
  }
  return "ignored";
}
console.log(onClick({ target: "del", preventDefault() { this.ok = true; } }));

Event Handlers — listeners react to events; delegation handles many children from one parent; preventDefault stops default browser action.

How to Explain Event Handlers to an Interviewer

What is Event Handlers (js)?

Used for a short Event Handlers js example a script in the browser Not just the heading

Give one small example of Event Handlers (js).

the smallest Event Handlers js example you can type onclick attributes vs addEventListener()

What mistake do beginners make with Event Handlers (js)?

only saying “Event Handlers js” with no example Show the fix

Where do you use Event Handlers (js)?

a script in the browser a short Event Handlers js example