JavaScript provides a wide array of mathematical operations and formulas to perform
calculations. Here are some of the most commonly used calculations and methods in
JavaScript:
1. Basic Arithmetic Operations
These are fundamental operations for calculations.
javascript
Copy code
let a = 10;
let b = 5;
let addition = a + b; // 15
let subtraction = a - b; // 5
let multiplication = a * b; // 50
let division = a / b; // 2
let modulus = a % b; // 0
let exponentiation = a ** b;// 100000
2. Increment and Decrement
These operations increase or decrease a value by 1.
javascript
Copy code
let count = 10;
count++; // 11
count--; // 10
3. Math Object
The Math object provides properties and methods for mathematical constants and
functions.
a. Constants
Math.PI: Represents the ratio of the circumference of a circle to its diameter,
approximately 3.14159.
Math.E: Represents Euler's number, the base of natural logarithms, approximately
2.718.
javascript
Copy code
console.log(Math.PI); // 3.141592653589793
console.log(Math.E); // 2.718281828459045
b. Methods
Math.abs(x): Returns the absolute value of x.
javascript
Copy code
console.log(Math.abs(-10)); // 10
Math.ceil(x): Returns the smallest integer greater than or equal to x.
javascript
Copy code
console.log(Math.ceil(4.3)); // 5
Math.floor(x): Returns the largest integer less than or equal to x.
javascript
Copy code
console.log(Math.floor(4.7)); // 4
Math.round(x): Returns the value of x rounded to the nearest integer.
javascript
Copy code
console.log(Math.round(4.5)); // 5
Math.max(x, y, ...): Returns the largest of zero or more numbers.
javascript
Copy code
console.log(Math.max(1, 3, 2)); // 3
Math.min(x, y, ...): Returns the smallest of zero or more numbers.
javascript
Copy code
console.log(Math.min(1, 3, 2)); // 1
Math.sqrt(x): Returns the square root of x.
javascript
Copy code
console.log(Math.sqrt(16)); // 4
Math.cbrt(x): Returns the cube root of x.
javascript
Copy code
console.log(Math.cbrt(27)); // 3
Math.pow(base, exponent): Returns the base to the exponent power, i.e.,
base^exponent.
javascript
Copy code
console.log(Math.pow(2, 3)); // 8
Math.random(): Returns a pseudo-random number between 0 (inclusive) and 1
(exclusive).
javascript
Copy code
console.log(Math.random()); // e.g., 0.234532
Math.sin(x), Math.cos(x), Math.tan(x): Return the sine, cosine, and tangent of x (x
is in radians).
javascript
Copy code
console.log(Math.sin(Math.PI / 2)); // 1
console.log(Math.cos(0)); // 1
console.log(Math.tan(Math.PI / 4)); // 1
Math.log(x): Returns the natural logarithm (base e) of x.
javascript
Copy code
console.log(Math.log(Math.E)); // 1
Math.log10(x): Returns the base 10 logarithm of x.
javascript
Copy code
console.log(Math.log10(100)); // 2
Math.exp(x): Returns e^x, where x is the argument, and e is Euler's number
(approximately 2.718).
javascript
Copy code
console.log(Math.exp(1)); // 2.718281828459045
Math.hypot(x, y, ...): Returns the square root of the sum of squares of its
arguments.
javascript
Copy code
console.log(Math.hypot(3, 4)); // 5
4. Trigonometric Functions
These functions are useful for calculations involving angles.
Math.asin(x): Returns the arcsine (in radians) of a number.
Math.acos(x): Returns the arccosine (in radians) of a number.
Math.atan(x): Returns the arctangent (in radians) of a number.
Math.atan2(y, x): Returns the arctangent of the quotient of its arguments.
javascript
Copy code
console.log(Math.asin(1)); // 1.5707963267948966 (PI / 2)
console.log(Math.acos(1)); // 0
console.log(Math.atan(1)); // 0.7853981633974483 (PI / 4)
console.log(Math.atan2(1, 1)); // 0.7853981633974483 (PI / 4)
5. Exponential and Logarithmic Functions
These are crucial for scientific calculations.
Math.log1p(x): Returns the natural logarithm (base e) of 1 + x.
Math.expm1(x): Returns e^x - 1.
javascript
Copy code
console.log(Math.log1p(1)); // 0.6931471805599453
console.log(Math.expm1(1)); // 1.718281828459045
6. Hyperbolic Functions
These functions are analogs of trigonometric functions for hyperbolic geometry.
Math.sinh(x): Returns the hyperbolic sine of x.
Math.cosh(x): Returns the hyperbolic cosine of x.
Math.tanh(x): Returns the hyperbolic tangent of x.
javascript
Copy code
console.log(Math.sinh(1)); // 1.1752011936438014
console.log(Math.cosh(1)); // 1.5430806348152437
console.log(Math.tanh(1)); // 0.7615941559557649
Summary
JavaScript provides a comprehensive set of mathematical tools through basic
operators and the Math object, allowing for a wide range of calculations from
simple arithmetic to complex trigonometric and logarithmic operations. These
capabilities make it possible to handle various mathematical needs directly within
your JavaScript code.