JAVASCRIPT PROJECT 02

Build a JavaScript Calculator

In this tutorial, you will learn how to build a calculator from the beginning. We will create the HTML structure, design it with CSS, and then use JavaScript to perform mathematical calculations.

01

Project Introduction

A calculator accepts numbers and mathematical operators and produces a result. Our JavaScript calculator will support addition, subtraction, multiplication, division and percentage operations.

What You Will Learn

  • HTML buttons
  • CSS Grid
  • JavaScript variables
  • Functions
  • Events
  • DOM manipulation
  • Mathematical operators
  • Keyboard events
HTML Creates the calculator structure.
CSS Creates the calculator design.
JavaScript Performs the calculations.
02

Create the HTML Structure

First, create a container for the calculator. Inside it we will place the display and buttons.

<div class="calculator">

    <div class="display">
        <div id="previous"></div>
        <div id="current">0</div>
    </div>

    <div class="buttons">

        <button>7</button>
        <button>8</button>
        <button>9</button>
        <button>+</button>

    </div>

</div>
The calculator has two important sections: the display and the buttons.
03

Create the Calculator Display

The display shows the number currently being entered and the previous calculation information.

<div class="display">

    <div id="previous"></div>

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

</div>

Why do we use IDs?

JavaScript can use these IDs to find the elements and change their content.

04

Create Calculator Buttons

Now create buttons for numbers and mathematical operators.

<button onclick="appendNumber('7')">
    7
</button>

<button onclick="appendNumber('8')">
    8
</button>

<button onclick="appendNumber('9')">
    9
</button>

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

Create JavaScript Variables

We need variables to remember the current number, previous number and selected operator.

let currentNumber = "";

let previousNumber = "";

let operator = null;

What do they store?

  • currentNumber → current number
  • previousNumber → previous number
  • operator → selected mathematical operator
06

Add Numbers Using a Function

When the user clicks a number, we need to add that number to the current value.

function appendNumber(number){

    currentNumber += number;

}

Connect the Button

<button onclick="appendNumber('5')">
    5
</button>
Clicking 5 calls appendNumber("5"), which adds 5 to the current number.
07

Handle Mathematical Operators

Next, create a function that remembers which operator the user selected.

function chooseOperator(selectedOperator){

    previousNumber = currentNumber;

    currentNumber = "";

    operator = selectedOperator;

}

For example, if the user enters 10 +, the number 10 becomes the previous number and + becomes the selected operator.

08

Calculate the Result

Now we need a function that performs the actual mathematical operation.

function calculate(){

    const first =
        parseFloat(previousNumber);

    const second =
        parseFloat(currentNumber);

    let result;

}

Use switch

A switch statement can check which operator was selected.

switch(operator){

    case "+":
        result = first + second;
        break;

    case "-":
        result = first - second;
        break;

    case "*":
        result = first * second;
        break;

    case "/":
        result = first / second;
        break;

}
parseFloat() converts the entered text into a number so JavaScript can perform mathematical operations.
09

Update the Calculator Display

After calculating the result, JavaScript must show the result on the webpage.

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

display.textContent =
    result;

Create an Update Function

function updateDisplay(){

    display.textContent =
        currentNumber || "0";

}
DOM manipulation allows JavaScript to change HTML content without reloading the page.
10

Add Clear and Delete

A calculator should allow the user to remove the last digit and reset everything.

Delete Function

function deleteNumber(){

    currentNumber =
        currentNumber.slice(0,-1);

    updateDisplay();

}

Clear Function

function clearCalculator(){

    currentNumber = "";

    previousNumber = "";

    operator = null;

    updateDisplay();

}
11

Add Keyboard Support

We can also allow users to operate the calculator using their keyboard.

document.addEventListener(
    "keydown",
    function(event){

        if(event.key === "Enter"){

            calculate();

        }

    }
);

Useful Keyboard Keys

  • Number keys → Enter numbers
  • + - * / → Operators
  • Enter → Calculate
  • Backspace → Delete
  • Escape → Clear

How Everything Works Together

HTML Structure
CSS Design
Input Numbers
Logic Calculate
DOM Result
12

Complete Calculator

You have now learned the main concepts required to build the calculator. The complete working version contains all of these concepts together.

Calculator Tutorial Completed! 🧮

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

Run Complete Calculator Learn JavaScript Operators