JavaScript counter

A counter combines a variable, a click event, and a DOM update.

What you will learn

Minimal example

<button id="plus" type="button">+1</button> <span id="value">0</span> <script>let count = 0; plus.onclick = () => value.textContent = ++count;</script>

Each click increases count, then writes the new value into the span.

Common mistakes