Category(s)
Topic(s)
The JavaScript Math object is a built-in object that provides mathematical functions and constants. It is not a constructor, so you cannot create instances of it. Instead, you access its methods and properties directly.
Rounding Methods
Math.round(x)
→ Rounds x to the nearest integer.Math.floor(x)
→ Rounds x down to the nearest integer.Math.ceil(x)
→ Rounds x up to the nearest integer.Math.trunc(x)
→ Removes the decimal part of x.
Example
const num = 4.7;
console.log(Math.round(num)); // Output: 5 (Rounds to nearest integer)
console.log(Math.floor(num)); // Output: 4 (Rounds down)
console.log(Math.ceil(num)); // Output: 5 (Rounds up)
console.log(Math.trunc(num)); // Output: 4 (Removes decimal part)
Arithmetic Methods
Math.pow(x, y)
→ Returns x raised to the power of y.Math.sqrt(x)
→ Returns the square root of x.Math.cbrt(x)
→ Returns the cube root of x.Math.abs(x)
→ Returns the absolute value of x.
Example
const x = -8;
const y = 3;
console.log(Math.pow(x, y)); // Output: -512 (Raises -8 to the power of 3)
console.log(Math.sqrt(16)); // Output: 4 (Square root of 16)
console.log(Math.cbrt(27)); // Output: 3 (Cube root of 27)
console.log(Math.abs(x)); // Output: 8 (Absolute value of -8)
Trigonometric Methods
Math.sin(x)
,Math.cos(x)
,Math.tan(x)
→ Returns trigonometric values (input in radians).Math.asin(x)
,Math.acos(x)
,Math.atan(x)
→ Returns inverse trigonometric values.Math.atan2(y, x)
→ Returns the angle between (x, y) and the x-axis.
Example
const x = 1;
const y = 1;
console.log(Math.sin(Math.PI / 2)); // Output: 1
console.log(Math.cos(0)); // Output: 1
console.log(Math.tan(Math.PI / 4)); // Output: 1
console.log(Math.asin(1)); // Output: 1.5707963267948966 (π/2)
console.log(Math.acos(0)); // Output: 1.5707963267948966 (π/2)
console.log(Math.atan(1)); // Output: 0.7853981633974483 (π/4)
console.log(Math.atan2(y, x)); // Output: 0.7853981633974483 (π/4)
Logarithmic and Exponential Methods
Math.log(x)
→ Natural logarithm (base e).Math.log10(x)
→ Logarithm base 10.Math.log2(x)
→ Logarithm base 2.Math.exp(x)
→ Returns e^x.
Example
const x = 8;
console.log(Math.log(x)); // Output: 2.0794415416798357 (Natural log of 8)
console.log(Math.log10(x)); // Output: 0.9030899869919435 (Log base 10 of 8)
console.log(Math.log2(x)); // Output: 3 (Log base 2 of 8)
console.log(Math.exp(2)); // Output: 7.38905609893065 (e^2)
Random and Max/Min Methods
Math.random()
→ Returns a random number between 0 and 1.Math.max(a, b, c, ...)
→ Returns the largest number.Math.min(a, b, c, ...)
→ Returns the smallest number.
Example
const randomNum = Math.random();
const maxNum = Math.max(10, 25, 5, 40, 15);
const minNum = Math.min(10, 25, 5, 40, 15);
console.log(randomNum); // Output: (random value between 0 and 1)
console.log(maxNum); // Output: 40 (largest number)
console.log(minNum); // Output: 5 (smallest number)
Generating a Random Number Between min (Inclusive) and max (Exclusive)
The function below returns a random integer where min
is included, but max
is excluded:
function getRandomInteger(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
// Testing the function
console.log(getRandomInteger(1, 10)); // Output: A random number between 1 and 9
console.log(getRandomInteger(20, 30)); // Output: A random number between 20 and 29
Explanation:
Math.random()
generates a random decimal between 0 and 1.- Multiplying by (
max - min
) scales the range. Math.floor()
rounds down to ensure the number remains within the specified range.- Finally,
min
is added so the lowest possible value ismin
.
Generating a Random Number Between min and max (Both Inclusive)
The function below ensures that both min
and max
are included in the possible outcomes.
function getRandomIntegerInclusive(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
// Testing the function
console.log(getRandomIntegerInclusive(1, 10)); // Output: A random number between 1 and 10
console.log(getRandomIntegerInclusive(50, 60)); // Output: A random number between 50 and 60
Explanation:
- The key difference here is (
max - min + 1
), which ensuresmax
is included in the possible results. - The rest of the function follows the same logic as the previous example.
Common Constants in Math Object
Math.PI
→ Approx. 3.14159Math.E
→ Euler’s number (2.718)Math.SQRT2
→ Square root of 2Math.LN2
→ Natural log of 2