JavaScript Program to perform arithmetic operations using switch case
In this program, You will learn how to perform arithmetic operations using a switch case in JavaScript.
switch(expression) {
case 1:
// statement
break
case 2:
// statement
break
default:
// statement
}
Example: How to perform arithmetic operations using a switch case in JavaScript
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)
}
Output:
Enter 1 For Addition:
Enter 2 For Subtraction:
Enter 3 For Multiplication:
Enter 4 For Division :
Enter 5 For Modulus :
Enter your choice:> 1
Enter first number> 40
Enter second number> 9
Result is :49