JavaScript Program to Perform Simple Mathematical Calculation Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like Report In this article, we are going to learn how perform simple mathematical calculations by using JavaScript. In this, we will get the two numbers and one operator as input and then the program will calculate the result according to the provided inputs. Example 1: Input : Operation : '+' num1 : 5 num2 : 6 Output : 11 Explanation: '+' operator is used to add two values so , 5+6 will be 11. Example 2: Input : Operation : '-' num1 : 5 num2 : 6 Output : -1 Explanation: '-' operator is used to subtract a value from another one so, 5-6 will be -1. These are the following approaches by using we can make a simple mathematical calculations: Using If-Else statementUsing switch caseSimple Calculations using If-Else Statement in JavaScriptCreate 4 functions for each operation i.e. '+', '-', '*', '/'.In the add function add the two numbers using the '+' operator.In the subtract function subtract the two numbers using the '-' operator.In the multiply function multiply the two numbers using the '*' operator.In the divide function divide the two numbers using the '/' operator.Take the operation, both numbers num1 & num2 as input from user.Check if the operation is '+' then call the add function. If the operation is '-' then call the subtract function. If the operation is '*' then call the multiply function. If the operation is '/' then call the divide function.Else return Invalid operation if user entered a wrong input. Example: JavaScript // JavaScript program for // simple mathematical calculations // Add two numbers function add(num1, num2) { return num1 + num2; } // Function for subtraction function subtract(num1, num2) { return num1 - num2; } // For multiplying of two numbers function multiply(num1, num2) { return num1 * num2; } // Function for division of // two numbers function divide(num1, num2) { if(num2 === 0) return undefined; return num1 / num2; } // Creating variables for // num1 and num2 let num1 = 16; let num2 = 4; let operation = "/"; // Creating result variable let result; // If-Else conditions if (operation === "+") { result = add(num1, num2); } else if (operation === "-") { result = subtract(num1, num2); } else if (operation === "*") { result = multiply(num1, num2); } else if (operation === "/") { result = divide(num1, num2); } else { result = "Invalid operation"; } // Printing the final result console.log("The Result of this operation is : " + result); OutputThe Result of this operation is : 4 Time complexity: O(1) Space complexity: O(1) Simple Calculations using Switch Statement in JavaScriptCreate 4 functions for each operation i.e. '+', '-', '*', '/'.In the add function add the two numbers using the '+' operator.In the subtract function subtract the two numbers using the '-' operator.In the multiply function multiply the two numbers using the '*' operator.In the divide function divide the two numbers using the '/' operator.Take the operation, both numbers num1 & num2 as input from user.Use the Switch Statement and take operation as argument.Case 1: if operation is '+' then call the add function.Case2: if the operation is '-' then call the subtract function. Case3: if the operation is '*' then call the multiply function.Case4: if the operation is '/' then call the divide function. Default Case: write 'Invalid Operation'. Print the Result. Example: JavaScript // JavaScript program for // simple mathematical calculations // Add two numbers function add(num1, num2) { return num1 + num2; } // Function for subtraction function subtract(num1, num2) { return num1 - num2; } // For multiplying of two numbers function multiply(num1, num2) { return num1 * num2; } // Function for division of // two numbers function divide(num1, num2) { if(num2 === 0) return undefined; return num1 / num2; } // Creating variable // for operation let operation = '+'; // Creating variables for // num1 and num2 let num1 = 4; let num2 = 5; // Creating result variable let result; // Switch case statement switch (operation) { case "+": result = add(num1, num2); break; case "-": result = subtract(num1, num2); break; case "*": result = multiply(num1, num2); break; case "/": result = divide(num1, num2); break; default: result = "Invalid operation"; } // Printing the final result console.log("The Result of this operation is : " + result); OutputThe Result of this operation is : 9 Time complexity: O(1) Space complexity: O(1) Comment More infoAdvertise with us Next Article JavaScript Program for Multiplication of Two Numbers G geek_suryansh Follow Improve Article Tags : JavaScript Web Technologies javascript-math JavaScript-DSA JavaScript-Program +1 More Similar Reads JavaScript Program to find simple interest What is âSimple Interestâ?Simple interest is a quick method of calculating the interest charge on a loan. Simple interest is determined by multiplying the daily interest rate by the principal by the number of days that elapse between payments.Simple Interest formula:Simple interest formula is given 2 min read JavaScript Program for Multiplication of Two Numbers In this article, we are going to learn how we can multiply two numbers using JavaScript. Multiplying the two numbers in JavaScript involves performing arithmetic addition on numeric values, resulting in their sum. JavaScript supports numeric data types. In JavaScript, both floating-point and integer 4 min read JavaScript program to find compound interest What is 'Compound interest' ? Compound interest is the addition of interest to the principal sum of a loan or deposit, or in other words, interest on interest. It is the result of reinvesting interest, rather than paying it out, so that interest in the next period is then earned on the principal sum 2 min read JavaScript Programs JavaScript Programs contains a list of articles based on programming. This article contains a wide collection of programming articles based on Numbers, Maths, Arrays, Strings, etc., that are mostly asked in interviews.Table of ContentJavaScript Basic ProgramsJavaScript Number ProgramsJavaScript Math 13 min read C Program to Make a Simple Calculator A simple calculator is a program that can perform addition, subtraction, multiplication, and division of two numbers provided as input. In this article, we will learn to create a simple calculator program in C.ExampleInput: a = 10, b = 5, op = +Output: 15.00Explanation: Chosen operation is addition, 3 min read Javascript Program for Prime Numbers What are prime numbers?A prime number is a natural number greater than 1, which is only divisible by 1 and itself. The first few prime numbers are 2 3 5 7 11 13 17 19 23...Prime numbersIn other words, the prime number is a positive integer greater than 1 that has exactly two factors, 1 and the numbe 5 min read Like