This project contains simple JavaScript functions that perform basic mathematical operations. It is useful for beginners learning how to write and use functions in JavaScript.
The program includes the following functions:
- โ Addition โ Adds two numbers
- โ Subtraction โ Finds the difference between two numbers
- โ๏ธ Multiplication โ Multiplies two numbers
- โ Division โ Divides two numbers (handles division by zero)
- ๐ฒ Square โ Calculates the square of a number
- โ Square Root โ Finds the square root of a number
function calculateSum(num1, num2) {
return num1 + num2;
}
2. Subtraction
function calculateDifference(num1, num2) {
return num1 - num2;
3. Multiplication
function calculateProduct(num1, num2) {
return num1 * num2;
}
4. Division (with error handling)
function calculateQuotient(num1, num2) {
return num2 === 0 ? "Error: Division by zero" : num1 / num2;
}
5. Square
function calculateSquare(num) {
return num ** 2;
}
6. Square Root
function calculateSquareRoot(num) {
return Math.sqrt(num);
}
โถ๏ธ Example Output
7
20
10
17
11
8
65
0.6363636363636364
Error: Division by zero
4
81
๐ How to Run
Copy the code into a file named mathFunctions.js
Run using Node.js:
node mathFunctions.js
๐ฏ Purpose
This project helps you understand:
Function creation in JavaScript
Parameters and return values
Basic arithmetic operations
Simple error handling
๐ ๏ธ Future Improvements
Add more advanced math functions (e.g., power, logarithms)
Create a user input interface
Convert into a calculator app