- Published on
Math In Javascript
- Authors
- Name
- Argenis De La Rosa
- @argenistherose
In JavaScript, you can create math equations using mathematical operators such as addition (+), subtraction (-), multiplication (*), division (/), and modulus (%). Here's a simple example of how to create and evaluate a math equation in JavaScript:
// Define variables
let num1 = 10;
let num2 = 5;
// Perform mathematical operations
let sum = num1 + num2; // Addition
let difference = num1 - num2; // Subtraction
let product = num1 * num2; // Multiplication
let quotient = num1 / num2; // Division
let remainder = num1 % num2; // Modulus (remainder)
// Output the results
console.log("Sum:", sum);
console.log("Difference:", difference);
console.log("Product:", product);
console.log("Quotient:", quotient);
console.log("Remainder:", remainder);
This code snippet defines two variables (num1 and num2) with numeric values and then performs various mathematical operations on them using the appropriate operators. Finally, it logs the results to the console.
You can create more complex equations by combining multiple operations and using parentheses to specify the order of operations. For example:
// Complex math equation
let result = (num1 + num2) * (num1 - num2) / 2;
console.log("Result:", result);
This equation adds num1 and num2, subtracts num2 from num1, then multiplies the result by 0.5, effectively calculating the average of num1 and num2.
JavaScript also provides built-in math functions like Math.pow() for exponentiation, Math.sqrt() for square roots, Math.sin() for sine, Math.cos() for cosine, etc., which can be used to perform more advanced mathematical calculations.