×

javascript · level 2 · unit 2 · lesson 2

  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

An is a function written right where it is needed.

Naming a function you only hand over once is like labelling a sandwich you are about to eat. An arrow function skips the name: () => { }. The round brackets hold any parameters, then comes the arrow =>, then the block.

Write it straight inside addEventListener, and the listener sits right next to the button it belongs to.

A variable made outside the listener lives on between clicks. Each click can add one to count and show the new total, so the page remembers.

<button id="like">Like</button>
<p id="likes">0 likes</p>
const button = document.querySelector("#like");
const likes = document.querySelector("#likes");
let count = 0;
button.addEventListener("click", () => {
  count++;
  likes.textContent = `${count} likes`;
});

Press run it to bring the page to life.

console

nothing logged yet

Use let for the count, because it changes. const would crash with a TypeError on the first click.