JAVASCRIPT PROJECT 04

Build a JavaScript Counter

In this project, you will learn how to build a counter using HTML, CSS and JavaScript. The counter will increase, decrease and reset its value when the user clicks buttons.

01

Project Introduction

A counter is one of the simplest JavaScript projects. It teaches an important programming concept: changing a value based on user interaction.

What You Will Learn

  • HTML buttons
  • JavaScript variables
  • Functions
  • Click events
  • DOM manipulation
  • Conditional statements
Increase Adds 1 to the counter.
Decrease Removes 1 from the counter.
Reset Returns the counter to zero.
02

Create the HTML Structure

First, create a container for the counter. Inside it we need a number display and buttons.

<div class="counter">

    <h2>
        Counter
    </h2>

    <div id="count">
        0
    </div>

    <button onclick="decrease()">
        −
    </button>

    <button onclick="reset()">
        Reset
    </button>

    <button onclick="increase()">
        +
    </button>

</div>
The display shows the current counter value, while the buttons control the value.
03

Create the Counter Display

The counter needs an element where the current number will be displayed.

<div id="count">
    0
</div>

The ID count will allow JavaScript to find this element.

04

Create the Counter Buttons

We need three buttons: decrease, reset and increase.

<button onclick="decrease()">
    −
</button>

<button onclick="resetCounter()">
    Reset
</button>

<button onclick="increase()">
    +
</button>
The onclick event calls a JavaScript function when the user clicks a button.
05

Create the Counter Variable

JavaScript needs a variable to remember the current counter value.

let count = 0;

The initial value is 0. Every button will change this value.

06

Create the Increase Function

To increase the counter, add 1 to the current value.

function increase(){

    count++;

}

Same Logic Using +1

function increase(){

    count = count + 1;

}
The ++ operator increases a number by one.
07

Create the Decrease Function

The decrease function subtracts 1 from the counter.

function decrease(){

    count--;

}

You can also write:

count = count - 1;
08

Create the Reset Function

Reset should return the counter value to zero.

function resetCounter(){

    count = 0;

}
Resetting a variable simply means assigning a new starting value to it.
09

Update the DOM

Changing the JavaScript variable alone does not automatically change the webpage. We need to update the HTML element.

const display =
    document.getElementById("count");

display.textContent =
    count;

Create an Update Function

function updateDisplay(){

    display.textContent =
        count;

}

Now call this function after increasing, decreasing or resetting the counter.

function increase(){

    count++;

    updateDisplay();

}
10

Add Value Limits

We can prevent the counter from going below zero by using an if statement.

function decrease(){

    if(count > 0){

        count--;

    }

    updateDisplay();

}

Maximum Value

We can also create a maximum value such as 100.

function increase(){

    if(count < 100){

        count++;

    }

    updateDisplay();

}
Conditional statements allow us to control how far the counter can increase or decrease.

Complete Project Flow

CLICK Button
FUNCTION Run Code
VARIABLE Change Value
DOM Update
OUTPUT New Count
12

Complete Counter Project

You have now learned how to create a JavaScript counter using variables, functions, events and DOM manipulation.

Tutorial Completed! 🔢

Now open the complete working Counter project and test what you have learned.

Run Complete Project Learn Increment & Decrement