How to make a counter in JavaScript?

To make a counter in JavaScript, start a timer with setInterval, increment the counter in it and set the value to a div.

So if this is your div for the counter:

<div id="counter"></div>

Then you can make it work with this JavaScript:

let i = 0;

setInterval(() => {
  document.getElementById('counter').textContent = ++i;
}, 1000);