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.
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
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>
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.
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>
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
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>
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.
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;
}
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";
}
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();
}
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
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