Back to blog
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
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.
- 1Add three buttons: Easy, Normal and Hard.
- 2Change the starting time for each one.
- 3Shrink the target button on Hard mode.
- 4Show a friendly message with the final score.
Keep going
Web Dev
Build Your First Web Page Today
HTML for the bones, CSS for the style: publish a page about yourself in under an hour.
Adam24 Sept 2026 · 6 min
PythonWeb Dev
Debugging Like a Pro (Even When You Are 10)
Errors are not failures, they are clues. Here is the routine real developers use to squash bugs.
Adam28 Jul 2026 · 4 min
PythonWeb DevRoblox
Ten Coding Projects to Build This Holiday
A project list sorted from easiest to boldest, with a clear finish line for every single one.
Mia15 Jul 2026 · 5 min