Calculator in Javascript with source code for free

Today, in this post we will be discussing how to create a calculator in javascript for free. In the last post, we discussed how to create a beautiful countdown timer in HTML, CSS, and javascript. Let’s see, how to create a calculator in javascript.

Calculator in Javascript:

asas

This calculator is a simple calculator and it is designed using CSS properties. It’s also working.

Html:

 As HTML is a markup language and we use this language to create the web page skeleton. We can only create the skeleton of the calculator in HTML but we can’t design it using Html.

CSS properties:

CSS has a huge list of properties and we can use these properties for free to design our website pages. We have designed the screen and buttons of the calculator with the help of CSS properties. This calculator has a soft UI.

You might like it:

Javascript libraries:

Javascript has a huge list of libraries. It is used for various purposes. We have used the javascript libraries just to add the functionalities to our calculator buttons and switch from dark mode to light mode. Calculator cant work without javascript libraries. This is a basic calculator. So, if you want to create this type of simple calculator in javascript then follow us.

How to create a calculator in javascript:

Follow below steps

Download a code editor:

It is the basic need for creating this type of project in HTML, CSS, and javascript.

Html source code:

Now save the file by using the extension .html at the end of the file name. This extension will understand the browser and code editor to understand the type of file.

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Calculator UI - FantacyDesigns </title>
    <link href='https://unpkg.com/[email protected]/css/boxicons.min.css' rel='stylesheet'>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="assets/css.css">
</head>

<body>

    <div class="container">
        <div class="iphone-x">
            <div class="screen">
                <div class="notch">
                    <div class="sound"></div>
                    <div class="camera"></div>
                </div>
                <div class="time" id="time">
                    <script>
                        var time = document.getElementById('time').innerHTML;
                        var today = new Date();
                        var time = today.getHours() + ":" + today.getMinutes();
                        time = document.write(time);
                    </script>
                </div>
                <div class="battery">
                    <i class="fa fa-battery-full"></i>
                </div>
                <div class="wifi">
                    <i class="fa fa-wifi"></i>
                </div>

                <div class="top-nav"></div>

                <form class="calculator container">
                    <input type="text" placeholder="0" name="displayResult" class="area" />

                    <div class="key container" id="keyBoard">

                        <div class="first-col"></div>

                        <div class="second-col">
                            <div class="button">
                                <button type="button" name="" value="AC" class="btn-work"
                                    onclick="displayResult.value=' '" id="c">
                                    AC
                                </button>
                            </div>
                            <div class="button">
                                <button type="button" name="" value="7" onclick="calcNumbers(this.value)" id="num">
                                    7
                                </button>
                            </div>
                            <div class="button">
                                <button type="button" name="" value="4" onclick="calcNumbers(this.value)" id="num">
                                    4
                                </button>
                            </div>
                            <div class="button">
                                <button type="button" name="" value="1" onclick="calcNumbers(this.value)" id="num">
                                    1
                                </button>
                            </div>
                            <div class="button">
                                <button type="button" name="" value="0" onclick="calcNumbers(this.value)" id="num">
                                    0
                                </button>
                            </div>
                        </div>
                        <div class="third-col">
                            <div class="button">
                                <button type="button" name="" value="+/-" onclick="makeNegative(displayResult.value)"
                                    class="btn-work" id="c">
                                    +/-
                                </button>
                            </div>
                            <div class="button">
                                <button type="button" name="" value="8" onclick="calcNumbers(this.value)" id="num">
                                    8
                                </button>
                            </div>
                            <div class="button">
                                <button type="button" name="" value="5" onclick="calcNumbers(this.value)" id="num">
                                    5
                                </button>
                            </div>
                            <div class="button">
                                <button type="button" name="" value="2" onclick="calcNumbers(this.value)" id="num">
                                    2
                                </button>
                            </div>
                            <div class="button">
                                <button type="button" name="" value="." onclick="calcNumbers(this.value)"
                                    class="btn-work" id="num">
                                    .
                                </button>
                            </div>
                        </div>
                        <div class="fourth-col">
                            <div class="button">
                                <button type="button" name="" value="%" onclick="calcNumbers(this.value)"
                                    class="btn-work" id="c">
                                    <i class="fa fa-percent"></i>
                                </button>
                            </div>
                            <div class="button">
                                <button type="button" name="" value="9" onclick="calcNumbers(this.value)" id="num">
                                    9
                                </button>
                            </div>
                            <div class="button">
                                <button type="button" name="" value="6" onclick="calcNumbers(this.value)" id="num">
                                    6
                                </button>
                            </div>
                            <div class="button">
                                <button type="button" name="" value="3" onclick="calcNumbers(this.value)" id="num">
                                    3
                                </button>
                            </div>
                            <div class="button">
                                <button type="button" name="" value="+" onclick="calcNumbers(this.value)"
                                    class="btn-work" id="num">
                                    <i class="fa fa-plus"></i>
                                </button>
                            </div>
                        </div>
                        <div class="fifth-col">
                            <div class="button">
                                <button type="button" name="" value="/" onclick="calcNumbers(this.value)"
                                    class="btn-work" id="eql"><i class="fa fa-divide"></i>
                                </button>
                            </div>
                            <div class="button">
                                <button type="button" name="" value="*" onclick="calcNumbers(this.value)"
                                    class="btn-work" id="eql"><i class="fa fa-times"></i>
                                </button>
                            </div>
                            <div class="button">
                                <button type="button" name="" value="-" onclick="calcNumbers(this.value)"
                                    class="btn-work" id="eql"><i class="fa fa-minus"></i>
                                </button>
                            </div>

                            <div class="button">
                                <button type="button" name="" value="="
                                    onclick="(displayResult.value.includes('%'))?percentage(displayResult.value):displayResult.value=eval(displayResult.value);"
                                    class="btn-equal" id="eql">
                                    =
                                </button>
                            </div>
                        </div>

                        <div class="menu"><i class='bx bx-menu-alt-left'></i></div>
                        <div class="home"><i class='bx bx-home-alt-2'></i></div>
                        <div class="back"><i class='bx bx-arrow-back'></i></div>

                    </div>
                </form>

            </div>
        </div>
    </div>

    <div class="drak-light">
        <i class="bx bx-moon moon"></i>
        <i class="bx bx-sun sun"></i>
    </div>

    <script src="assets/js.js"></script>
</body>

</html>

CSS source code:

Again create a new file for CSS. The compiler and code editor will understand the type of the file by using the extension of .css at the end of the file name.

@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500&display=swap');

:root
{
    --bodybg:#e7e8ee;
    --btt:#222222;
    --tcolor: rgb(119 119 119);

    --BxSi:
        6px 6px 8px rgba(13, 39, 80, 0.25),
        -6px -6px 10px #fff,
        inset -8px -8px 12px rgba(255,255,255,0.7),
        inset 5px 5px 8px rgba(13,39,80,0.2);

    --PSD-3d:
        7px 7px 15px #45566754,
        -8px -8px 15px #5165790a,
         inset -5px -4px 13px 0px #51657959,
         inset 6px 7px 12px 0px #fffffff0;

    --eql-background:
    linear-gradient(to bottom right, rgb(211 109 18),rgb(237 157 30));

    --eql-shadow:
    inset 1px 1px 3px rgb(249 133 0),
    inset 4px 4px 15px rgb(179 83 0),
    inset -1px -1px 3px rgb(204 99 8),
    3px 3px 5px rgb(194 199 212);

    --c-background: linear-gradient(to bottom right, rgb(139 148 161),rgb(239 240 247));
    --c-shadow:
    inset 1px 1px 1px rgb(200 206 217),
    inset 2px 2px 8px rgb(202 207 214),
    inset -2px -2px 3px rgb(180 187 199),
    3px 3px 12px rgb(194 199 212);

    --num-background:
    linear-gradient(to bottom right, rgb(192 197 209),rgb(237 238 245));

    --num-shadow:
    inset 2px 2px 3px rgb(235 235 243),
    inset 4px 4px 15px rgb(202 207 214),
    3px 3px 12px rgb(194 199 212);

}

body.dark{


    --bodybg:#1e212a;
    --btt:#fff;
    --tcolor: rgb(119 119 119);

    --BxSi:6px 6px 8px rgb(0 0 0 / 50%),
        -6px -6px 10px rgb(255 255 255 / 5%),
         inset -8px -8px 12px rgb(255 255 255 / 5%),
         inset 5px 5px 8px rgb(0 0 0 / 50%);

    --PSD-3d:
    5px 5px 15px rgb(0 0 0 / 18%),
    5px 15px 15px rgb(0 0 0 / 25%),
    inset 5px 5px 10px rgb(0 0 0 / 50%),
    inset 5px 5px 20px rgb(255 255 255 / 20%),
    inset -5px -5px 15px rgb(0 0 0 / 75%);


    --eql-shadow:
    inset 1px 1px 2px rgb(249 170 45 / 60%),
     inset 4px 4px 15px rgb(165 78 4),
      3px 3px 5px rgb(24 27 36);

    --eql-shadow:
    inset 1px 1px 2px rgb(249 170 45 / 60%),
     inset 4px 4px 15px rgb(165 78 4),
      3px 3px 5px rgb(24 27 36);

    --c-background: linear-gradient(to bottom right, rgb(73 79 92),rgb(111 117 135));
    --c-shadow:
     inset 1px 1px 1px rgb(101 107 123),
      inset 4px 4px 15px rgb(51 57 66),
       3px 3px 5px rgb(24 27 36);

    --num-background: linear-gradient(to bottom right, rgb(41 45 56),rgb(60 60 68));
    --num-shadow:
    inset 1px 1px 1px rgb(57 60 74),
     inset 4px 4px 15px rgb(32 35 45),
      3px 3px 5px rgb(24 27 36);

}

/***  ***/
*
{
	margin: 0;
	padding: 0;
	font-family: 'Roboto', sans-serif;
}

body
{
    background: var(--bodybg);
}
.iphone-x
{
    margin-top: 100px;
}
.iphone-x .screen
{
    width: 42%;
    height: 640px;
    margin: 0 auto;
    min-width: 310px;
    max-width: 310px;
    border-radius: 44px;
    background: var(--bodybg);
    padding: 10px;
    box-shadow:var(--PSD-3d);
}
.screen .notch
{
    width: 159px;
    height: 30px;
    margin: 0 auto;
   /** background: red;**/
    border-bottom-right-radius: 15px;
    border-bottom-left-radius: 15px;
}
.sound
{
    top: 10px;
    left: 54px;
    width: 40px;
    height: 6px;
    position: relative;
    background: var(--btt);
    border-radius: 10px;
}
.camera
{
    top: 2px;
    left: 110px;
    width: 10px;
    height: 10px;
    position: relative;
    background: var(--btt);
    border-radius: 100px;
}
.time
{
    color: var(--btt);
    width: 60px;
    font-size: 11px;
    margin-top: -22px;
    background: transparent;
    margin-left: 18px;
}
.battery
{
    margin-top: -20px;
}
.battery i
{
    color: var(--btt);
    margin-left: 230px !important;
}
.wifi
{
    margin-top: -18px !important;
    margin-left: 260px;

}
.wifi i
{
    color: var(--btt);
    margin-top: -20px;
}

.area
{
    min-width: 50% !important;
    width: 94% !important;
    min-height: 120px !important;
    margin-left: 6px;
    margin-top: -5px;
    font-size: 30px !important;
    text-align: right;
    overflow: scroll;
    outline: none;
    border: none;
    font-weight: 400;
    color: var(--btt);
    background: var(--bodybg);
    border-radius: 10px;
    padding: 2px;
    box-shadow: var(--BxSi);
}
.calculator
{
    margin: 0;
    margin-top: 38px;
    box-sizing: content-box;
}

.key
{
    float: left !important;
    font-size: 15px;
    display: flex !important;
    margin: -5%;
    margin-left: auto !important;
    margin-right: auto !important;
}
.fifth-col,
.fourth-col,
.third-col,
.second-col,
.first-col
{
    padding-left: 9px;
    display: inline-block;
    margin-top: 20px !important;
    justify-content: space-between !important;
}
.button
{
    display: block;
    padding-left: 40px;
    min-height: 40px;
    padding-right: 30px;
    white-space: wrap;
    word-wrap: break-word;
    margin-top: 20px;
    justify-content: space-between !important;
}
button
{
    position: relative;
    margin-left: -54px !important;
    min-width: 55px;
    align-items: center;
    justify-content: center;
    text-align: center;
    padding: 0px;
    color: var(--btt) !important;
    border: none;
    border-radius: 17px !important;
    height: 55px;
    outline: none;
    font-weight: 400 !important;
    font-size: 14pt !important;
}
.btn-work
{
    color: var(--btt) !important;
}
.btn-equal
{
    color: var(--btt) !important;
    height: 120px !important;
}
button:focus
{
    cursor: pointer;
    outline: none !important;
    border: none !important;
}
button:active
{
    outline: none !important;
    border: none !important;
}
button:hover
{
    color: #696a6a !important;
}
.btn-work:hover,
.btn-equal:hover
{
    color: #0b254e !important;
}
.home
{
    margin-top: 415px;
    margin-left: 66px;
    font-size: 30px;
    cursor: pointer;
    color:var(--btt);
}
.menu
{
    margin-top: 422px;
    margin-left: -280px;
    font-size: 20px;
    cursor: pointer;
    color:var(--btt);
}
.back
{
    margin-top: 422px;
    margin-left: 75px;
    font-size: 20px;
    cursor: pointer;
    color:var(--btt);
}

#eql
{
    background: var(--eql-background);
    box-shadow: var(--eql-shadow);
    color: #fff !important;
}

#c
{
    background: var(--c-background);
    box-shadow: var(--c-shadow);
}

#num
{
    background: var(--num-background);
    box-shadow: var(--num-shadow);
}

.drak-light
{
    left: 50px;
    display: flex;
    position: absolute;
    justify-content: center;
    top: 50px;
    width: 60px;
    height: 60px;
    background: var(--num-background);
    box-shadow: var(--num-shadow);
    text-align: center;
    align-items: center;
    border-radius: 50%;
}
.drak-light i
{
    position: absolute;
    font-size: 25px;
    cursor: pointer;
    transition: all 0.3s ease;
}
.drak-light i.sun
{
    opacity: 0;
    pointer-events: none;
}
.drak-light.active i.sun{
    opacity: 1;
    pointer-events:auto;
}
.drak-light.active i.moon{
    opacity: 0;
    pointer-events: none;
}
.bx-sun
{
    color: #ffd900;
}
.bx-moon
{
    color: #888;
}

Javascript source code:

function calcNumbers(result) {
    document.querySelector('.area').value += result;
}


function percentage(result) {
    var result1 = String(result);
    if (result1.includes("%")) {
        var a, b, percent;
        var splitWord = [];
        a = Number(splitWord[0]);
        b = Number(splitWord[1]);
        percent = (a / 100 * b);
        document.querySelector('.area').value = percent;
    }
}


function makeNegative(result) {
    if (result.charAt(0) == '_') {
        document.querySelector('.area').value = result.replace('-', '');
    }
    else {
        document.querySelector('.area').value = '-' + result;
    }
}



const body = document.querySelector("body"),
modeToggle = document.querySelector(".drak-light");

modeToggle.addEventListener("click" ,() =>{
    modeToggle.classList.toggle("active");
    body.classList.toggle("dark");
    if(!body.classList.contains("dark"))
    {
        localStorage.setItem("mode", "light-mode");
    }
    else
    {
        localStorage.setItem("mode", "dark-mode");
    }  
});


Download source code:

If you are unable to use or connect the files with each other. Then we created a solution. The download button is given below.

If you have any questions related to the calculator in javascript then ask these questions in the comment section. We will try to clear your doubts related to the calculator.

Tasks and Conclusion:

It’s a great step to create a calculator in javascript. It will help you to understand a lot of libraries of javascript and properties of CSS. Now I am assigning you some tasks and they will lead you toward a great learning journey. 

You have to change the screen size. Then change the position of the + button from the right below corner to the upper left corner. Also, change the color of these buttons. All the buttons have round corners. Just remove that from the buttons that made them round from corners. By doing all of these things you can create beautiful logic in your programming skill.

Thanks for reading this article.

[su_divider divider_color=”#1dbf73″ link_color=”#1dbf73″]

[su_button url=”https://mega.nz/file/x8s3GSKR#AbKs23DKhvSPQlOMHdrXLd99F2Z6oYOyAD1PnKEMtnk” target=”blank” style=”3d” background=”#1dbf73″ size=”6″ radius=”5″ desc=”Click to Download” id=”download”]Download Source Code[/su_button]

Leave a Comment

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

Scroll to Top