×

javascript · level 2 · unit 2 · lesson 1

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

runs your function each time an happens.

Clicks, key presses and typing are all events, and your code can listen for them. button.addEventListener("click", cheer) means: every time this button is clicked, call cheer.

A function waiting like this is a . Notice there are no brackets after cheer. With brackets, cheer() would run once, straight away. Without them, you hand the function over, and the browser calls it on every click.

A function handed over to be called later is a , like leaving your number so a friend can ring you back.

<button id="cheer">Cheer</button>
<p id="crowd">The crowd is quiet.</p>
const button = document.querySelector("#cheer");
const crowd = document.querySelector("#crowd");
function cheer() {
  crowd.textContent = "Hip hip hooray!";
}
button.addEventListener("click", cheer);

Press run it to bring the page to life.

console

nothing logged yet

Press run, then click the button. Nothing happens until you do.