Back to blog
Web Dev

Code a Click Game with JavaScript

Twenty lines of JavaScript, one button and a timer: your first playable browser game.

Mia 2 September 2026 6 min read
Code a Click Game with JavaScript

JavaScript is the language that makes web pages react. Buttons, scores and timers all come alive with it.

The page

html
 1<button id="target">Click me!</button>
 2<p>Score: <span id="score">0</span></p>
 3<p>Time: <span id="time">10</span>s</p>

The game logic

javascript
 1let score = 0;
 2let time = 10;
 3
 4const button = document.getElementById("target");
 5
 6button.addEventListener("click", () => {
 7  score += 1;
 8  document.getElementById("score").textContent = score;
 9});
10
11setInterval(() => {
12  time -= 1;
13  document.getElementById("time").textContent = time;
14  if (time <= 0) {
15    button.disabled = true;
16    button.textContent = "Game over!";
17  }
18}, 1000);

Make it feel great

  • Move the button to a random spot after every click.
  • Play a short sound when the score goes up.
  • Save the best score with localStorage so it survives a refresh.

Try it yourself

Add difficulty levels to your click game.

  1. 1Add three buttons: Easy, Normal and Hard.
  2. 2Change the starting time for each one.
  3. 3Shrink the target button on Hard mode.
  4. 4Show a friendly message with the final score.
Back to blog

Keep going