JavaScript Program to Make a Simple Calculator

To understand this example, you should have the knowledge of the following JavaScript programming topics:


Example 1: Simple Calculator with if..else if...else

// program for a simple calculator

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

// take the operand input
const number1 = parseFloat(prompt('Enter first number: '));
const number2 = parseFloat(prompt('Enter second number: '));

let result;

// using if...else if... else
if (operator == '+') {
    result = number1 + number2;
}
else if (operator == '-') {
    result = number1 - number2;
}
else if (operator == '*') {
    result = number1 * number2;
}
else {
    result = number1 / number2;
}

// display the result
console.log(`${number1} ${operator} ${number2} = ${result}`);

Output

Enter operator ( either +, -, * or / ): *
Enter first number: 3.4
Enter second number: 5.6
3.4 * 5.6 = 19.04

In the above example, the user is prompted to enter an operator (either +, -, * or /) and two numbers.

The parseFloat() converts the numeric string value to a floating-point value.

The if...else if...if statement is used to check the condition that the user has entered for the operator. The corresponding operation is performed and the output is displayed.


Example 2: Simple Calculator with switch

// program for a simple calculator
let result;

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

// take the operand input
const number1 = parseFloat(prompt('Enter first number: '));
const number2 = parseFloat(prompt('Enter second number: '));

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

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

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

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

    default:
        console.log('Invalid operator');
        break;
}

Output

Enter operator: +
Enter first number: 4
Enter second number: 5
4 + 5 = 9

In above program, the user is asked to enter either +, -, * or /, and two numbers. Then, the switch statement executes cases based on the user input.


Also Read:

Before we wrap up, let’s put your knowledge of JavaScript Program to Make a Simple Calculator to the test! Can you solve the following challenge?

Challenge:

Write a function to perform basic arithmetic operations.

  • The operations are: + for Addition, - for Subtraction, * for Multiplication and / for Division.
  • Given num1, num2, and op specifying the operation to perform, return the result.
  • For example, if num1 = 5, op = "+" and num2 = 3, the expected output is 8.
Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges