Arithmetic operations using switch case in JavaScript | Example code

  • by

Use numbers for add, subtract, multiply, or division math operations and pass those values into a switch statement. Then use arithmetic operations code in javascript switch case block.

Arithmetic operations using switch case in JavaScript

Simple example code performs arithmetic operations. Where use can choose to add, subtract, multiply, or division option along with 2 numbers. Then switch case takes those inputs and performs operations.

<!DOCTYPE html>
<html>
<body>
  <script>
    let x, y, res, ch

    console.log("Enter 1 For Addition:")
    console.log("Enter 2 For Subtraction:")
    console.log("Enter 3 For Multiplication:")
    console.log("Enter 4 For Division :")
    console.log("Enter 5 For Modulus :")
    
    ch = parseInt(prompt("Enter your choice:"))

    if (ch > 0 && ch < 6) {
      x = parseInt(prompt("Enter first number"))
      y = parseInt(prompt("Enter second number"))
    }

    switch (ch) {
      case 1:
      res = x + y;
      console.log("\nResult is :" + res)
      break

      case 2:
      res = x - y
      console.log("\nResult is :" + res)
      break

      case 3:
      res = x * y
      console.log("\nResult is :" + res)
      break

      case 4:
      res = x / y
      console.log("\nResult is :" + res)
      break

      case 5:
      res = x % y;
      console.log("\nResult is :" + res)
      break

      default:
      console.log("Invalid Choice:" + ch)
    }
  </script>

</body>
</html>

Output:

Arithmetic operations using switch case in JavaScript

Another example

<!DOCTYPE html>
<html>
<body>
  <input type="number" id="num1" placeholder="1. sayı">
  <input type="number" id="num2" placeholder="2. sayı">
  <div>
    <ol>
      <button onclick="calculate(this)" value="+">[ + ] Topla</button>
      <button onclick="calculate(this)" value="-">[ - ] Çıkar</button>
      <button onclick="calculate(this)" value="*">[ * ] Çarp</button>
      <button onclick="calculate(this)" value="/">[ / ] Böl</button>
    </ol>
  </div>

  <input type="number" id="sonuc" placeholder="Sonuç">

  <script>
    function calculate(button) {
      var num1 = +document.getElementById("num1").value;
      var num2 = +document.getElementById("num2").value;
      var operator = button.getAttribute('value');
      switch (operator) {
        case "+":
        document.getElementById("sonuc").value = num1 + num2;
        break;
        case "-":
        document.getElementById("sonuc").value = num1 - num2;
        break;
        case "*":
        document.getElementById("sonuc").value = num1 * num2;
        break;
        case "/":
        document.getElementById("sonuc").value = num1 / num2;
        break;
      }

    }
  </script>

</body>
</html
>

Do comment if you have any doubts or suggestions on this JS switch case.

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

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.