Skip to content

Calculator using switch case in JavaScript | Complete code

Simply take input “add”, “divide”, “multiply”, or “subtract” option form use along with 2 numbers then Calculator using switch case in JavaScript.

In this tutorial, you’ll learn how to build a calculator program in JavaScript using a switch case statement. This example demonstrates how to create a basic calculator that can perform addition, subtraction, multiplication, and division operations on two numbers based on the user’s input.

Calculator using switch case in JavaScript

Simple example code user is prompted to enter an operator (either +, , *, or /) and two numbers.

<!DOCTYPE html>
<html>
<body>

  <script>
    
    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;
      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;
    }
  </script>

</body>
</html
>

Output:

Calculator using switch case in JavaScript

You can simply add a button to call a function like this

<script>
    function Calculatrice(a, b, op) {

      switch (op) {

        case '+':
        return a + b
        break;
        case '-':
        return a - b
        break;
        case '*':
        return a * b
        break;
        case '/':
        return a / b
        break;
        
      }
    }
    console.log(Calculatrice(5, 5, '+'));
    
</script>

Output: 10

OR

function calculator(operator, num1, num2) {
  switch(operator) {
    case '+':
      return num1 + num2;
    case '-':
      return num1 - num2;
    case '*':
      return num1 * num2;
    case '/':
      return num1 / num2;
    default:
      return 'Invalid operator';
  }
}

// Example usage:
console.log(calculator('+', 2, 3)); // Output: 5
console.log(calculator('-', 5, 2)); // Output: 3
console.log(calculator('*', 4, 6)); // Output: 24
console.log(calculator('/', 10, 2)); // Output: 5
console.log(calculator('%', 10, 3)); // Output: 'Invalid operator'

In this example, we have a calculator function that takes three parameters: operator, num1, and num2. The operator parameter specifies the arithmetic operation to perform on num1 and num2. The switch statement checks the value of operator and performs the corresponding operation. If operator is not one of the specified values, the function returns an error message.

Comment if you have any doubts or suggestions on this JS switch case topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

1 thought on “Calculator using switch case in JavaScript | Complete code”

  1. hello, how can I use the switch statement to disable the alphabet keyboard?

    document.addEventListener(“keydown”, (event) => {
    console.log(event);

    switch (event.key) {
    case “C”:
    case “Backspace”:
    display.textContent = “”;
    break;

    case “ArrowUp”:
    case “ArrowDown”:
    case “ArrowLeft”:
    case “ArrowRight”:
    break;

    case “=”:
    display.textContent = eval(display.textContent);
    break;

    case “Enter”:
    display.textContent = eval(display.textContent);
    break;

    default:
    display.textContent += event.key;
    }
    });

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading