Write a program to build a button based UI Calculator with Javascript.

1
2
    <!DOCTYPE html>
3
    <html lang="en">
4
5
    <head>
6
        <meta charset="UTF-8">
7
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
8
        <title>Simple Calculator</title>
9
        <style>
10
            table {
11
                border-collapse: collapse;
12
                margin: 20px 0;
13
            }
14
            input[type="text"] {
15
                width: 100%;
16
                text-align: right;
17
                font-size: 1.2em;
18
                padding: 5px;
19
                box-sizing: border-box; /* Ensures the input does not overflow */
20
            }
21
            input[type="button"] {
22
                width: 50px;
23
                height: 50px;
24
                font-size: 1.1em;
25
                cursor: pointer;
26
            }
27
        </style>
28
    </head>
29
30
    <body>
31
        <h3>Simple Calculator</h3>
32
        <form name="calc">
33
            <table border="2">
34
                <tr>
35
                    <td colspan="4"><input type="text" name="display" readonly></td>
36
                </tr>
37
                <tr>
38
                    <td><input type="button" value="0" onclick="calc.display.value += '0'"></td>
39
                    <td><input type="button" value="1" onclick="calc.display.value += '1'"></td>
40
                    <td><input type="button" value="2" onclick="calc.display.value += '2'"></td>
41
                    <td><input type="button" value="+" onclick="calc.display.value += '+'"></td>
42
                </tr>
43
                <tr>
44
                    <td><input type="button" value="3" onclick="calc.display.value += '3'"></td>
45
                    <td><input type="button" value="4" onclick="calc.display.value += '4'"></td>
46
                    <td><input type="button" value="5" onclick="calc.display.value += '5'"></td>
47
                    <td><input type="button" value="-" onclick="calc.display.value += '-'"></td>
48
                </tr>
49
                <tr>
50
                    <td><input type="button" value="6" onclick="calc.display.value += '6'"></td>
51
                    <td><input type="button" value="7" onclick="calc.display.value += '7'"></td>
52
                    <td><input type="button" value="8" onclick="calc.display.value += '8'"></td>
53
                    <td><input type="button" value="×" onclick="calc.display.value += '*'"></td>
54
                </tr>
55
                <tr>
56
                    <td><input type="button" value="9" onclick="calc.display.value += '9'"></td>
57
                    <td><input type="button" value="C" onclick="calc.display.value = ''"></td>
58
                    <td><input type="button" value="=" onclick="try { calc.display.value = eval(calc.display.value) } catch { calc.display.value = 'Error' }"></td>
59
                    <td><input type="button" value="÷" onclick="calc.display.value += '/'"></td>
60
                </tr>
61
            </table>
62
        </form>
63
    </body>
64
65
    </html>
66
67
    

Output

1
2
        No Output Available
3
        

Rate the Code

Reviews are published after a quick safety check (usually within 24 hours).

Reviews are public. Developers can see basic device specifications to respond. Learn more

Core Concept of a Basic Calculator: Button-based Numbers and Operators in JavaScript

This program makes a basic calculator with the help of a grid-based table layout. An HTML form and inline JS (JS stands for JavaScript) event handlers (onClick) are used.

Working of this Program

  • Appending values: When the user clicks on any number (0-9) or operators (+, -, *, /), then calc. display.value += value; appends the value as a string.
  • Clearing Display: The ‘C’ button is used to clear user input.
  • Evaluation (eval()): When user clicks on “=” button then the JS eval() function calculates the final result of the string link “5+3*2”.

Why is it used?

This is a classic, lightweight, and self-contained example for DOM element targeting, form object reference, and string evaluation.

Real-Life Applications

  • Embed on a Web Page: To embed a small calculator in web pages, blogs, or student portals. For example, if your search on Google basic calculator then you will get a basic calculator on Google Search Result Page.
  • Educational Teaching Tools: To teach the concepts of JS main events, form binding, and expression evaluation.

Benefits of this Program

  • Zero Dependencies: There is no need for any extra framework, because this code is complete and acts as a web app.

More About This JavaScript Program for a Simple Calculator

This program demonstrates a simple calculator program with the help of HTML, CSS, and JavaScript technologies. In this program the calculator has four basic arithmetic operators as Addition, Subtraction, Multiplication and Division.

The user can provide input by pressing the buttons like 1, 2, 3, +, -, etc., and receive the output in the screen box.

Basically, this is a small simulation of a simple calculator that has the basic operators.

You can copy this code and modify it according to your requirements.

License: This program's source code is open-sourced under the MIT License, while the rest of the program's text content and overall dhimanp platform and brand content remain proprietary under dhimanp.

Submit your modified code

Your submissions are published after a quick safety check (usually within 7 days).

Submissions are public. Developers can see basic device specifications to respond. Learn more

Disclaimer

The information that you shared with us will be publicly visible, and everyone can see your information. And we (dhimanp) are not responsible if anyone misuses this information. We can remove any submitted code at any time for better transparency.

Privacy Policy

If you share your data with us by submitting your code for credit, then we may show your full name, school or college name, and city name on our website.

We are not using this data to track you and are not selling this data to anyone.

If you want to remove your name from our website, you can contact us, and we will remove your name from our website. In the removal process, full credit, including your full name, school or college, city, and code, will be removed from our website.