Repaso
Español

What does this do?

button.addEventListener("click", () => {
  console.log("clicked");
});

Options

  1. It clicks the button immediately on page load
  2. It registers a function that runs on every click (Correct option)
  3. It deletes the button from the page when clicked
  4. It logs a message once, when the page loads

Answer

Registers a function that runs every time the button is clicked

Why

addEventListener(eventName, callback) tells the browser: 'when this event happens on this element, call this function'.

The function is a callback: you give it to the browser, and the browser calls it later, once per click.

Other common events: "input", "submit", "keydown", "scroll".

Remember

addEventListener = run this function when that happens.

Key terms

Practice this question in Repaso