JavaScript Calculator Example

In this tutorial, I will tell you two ways to make a calculator using basic HTML, CSS and JavaScript. In first way we make functions for all operations and in the second we use some inbuilt global function to make a calculator. In both cases we are going to add some simple math operations like addition, subtraction, multiplication, division in our basic calculator. This project will help you to improve your logical ability and coding skills.

JavaScript Calculator Example

Method 1:

Mainly the functions that we need to work for calculator are handling the keypresses, inputting the digits, inputting the decimal point/key, handling operators, resisting the calculators, updating the display, equal key and clear key.

As we know calculator consists of keys and display. So, in the UI perspective we have to work on:

calculator tile – where we show the title

input buttons – where we display all keys

output screen – This is the screen where we can check what input we are entering and showing all the values that we entered.

Before starting this Project you must have knowledge or idea of these things:

  • if-else statements
  • javascript functions
  • arrow function
  • some operators like && and ||
  • change the text with the value attribute
  • add event listeners
  • querySelector
  • string methods like includes, contains etc.
  • eval function

So, coding part begins here for the first case. Initially, we have to make a div container in which we are going to add all buttons with proper styling. To make this UI we use CSS Grid box which is the best practice of writing proper responsive code. After this, we start work on functionalities.

Firstly we have to initialise the value of each variable and constant. Then we check what key is pressed by the user like operators, operands, decimal or clear according to this key we store it in different variables that we declared. So, when the user hits number keys then we store these values in operands. We define the separate function for each operation like inputting the digit, updating the display, decimal enter etc.

You can check the whole code below.

Simple JavaScript Calculator

Method 2:

We can also make a JavaScript calculator using the eval function. This function is used to evaluate any string. This is a built-in function of JS.

Here we used the table tag to make UI and for adding additional functions we use form HTML element. On each keypress, we put on click method to add that value to the output screen. And after this eval function evaluate all values when you click on any operator.

Simple JavaScript Calculator

Comment down below if you have any queries related to javascript calculator.

1 thought on “JavaScript Calculator Example”

  1. 1. how to prevent multiple dot or operator(+,*,-,/) in row . ex 1+*!
    2. how to clear display after getting result when we click on digit for new calculation.
    3. how to calculate % using this formula = (firstvalue/100)*second value.
    4. how to differentiate the firstvalue and secondvalue after and before the operator. ex 100%50 = 50 , here 100 is firstvalue , 50 is secondvalue and after evaluating ans is 50

Leave a Comment

Your email address will not be published. Required fields are marked *