How To Make a Simple Calculator Using JavaScript Example

Oct 28, 2021 . Admin



Hello Friends,

Now let's see example of how to make calculator example.We will check how to make calculator. This is a short guide on make calculator in javascript. Let's get started with how to make calculator in javascript.

Here i will give you many example how to make calculator using javascript.

Example : 1
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>How To Make a Simple Calculator Using JavaScript Example - MyWebtuts.com</title>
    </head>
    <body>
        <h2>How To Make a Simple Calculator Using JavaScript Example - MyWebtuts.com</h2>
        <script type="text/javascript">
            const operator = prompt('Enter operator ( either +, -, * or / ): ');
            const num1 = parseFloat(prompt('Enter first number : '));
            const num2 = parseFloat(prompt('Enter second number : '));
            let result;

            if (operator == '+') {
                result = num1 + num2;
            }
            else if (operator == '-') {
                result = num1 - num2;
            }
            else if (operator == '*') {
                result = num1 * num2;
            }
            else {
                result = num1 / num2;
            }
            document.write(`${num1} ${operator} ${num2} = ${result}`);
        </script>
    </body>
</html>
Output :
Enter operator ( either +, -, * or / ) : *
Enter first number : 12
Enter second number : 6
12 * 6 = 72
Example : 2
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>How To Make a Simple Calculator Using JavaScript Example - MyWebtuts.com</title>
    </head>
    <body>
        <h2>How To Make a Simple Calculator Using JavaScript Example - MyWebtuts.com</h2>
        <script type="text/javascript">
            let result;

            const operator = prompt('Enter operator ( either +, -, * or / ): ');

            const number1 = parseFloat(prompt('Enter first number: '));
            const number2 = parseFloat(prompt('Enter second number: '));

            switch(operator) {
                case '+':
                    result = number1 + number2;
                    document.write(`${number1} + ${number2} = ${result}`);
                break;

                case '-':
                    result = number1 - number2;
                    document.write(`${number1} - ${number2} = ${result}`);
                break;

                case '*':
                    result = number1 * number2;
                    document.write(`${number1} * ${number2} = ${result}`);
                break;

                case '/':
                    result = number1 / number2;
                    document.write(`${number1} / ${number2} = ${result}`);
                break;

                default:
                    document.write('Invalid operator');
                break;
            }
        </script>
    </body>
</html>
Output :
Enter operator ( either +, -, * or / ): -
Enter first number: 65
Enter second number: 44
65 - 44 = 21

It will help you...

#Javascript