Skip to content

JavaScript Math

The built-in Math object in JavaScript provides properties and methods for performing mathematical operations. It is not a constructor, so you don’t create instances of Math. All its properties and methods are static and accessed directly via Math.

JavaScript provides several built-in mathematical constants:

console.log(Math.PI); // Output: 3.141592653589793 (PI)
console.log(Math.E); // Output: 2.718281828459045 (Euler's number)
console.log(Math.SQRT2); // Output: 1.4142135623730951 (Square root of 2)
console.log(Math.SQRT1_2); // Output: 0.7071067811865476 (Square root of 1/2)
console.log(Math.LN2); // Output: 0.6931471805599453 (Natural logarithm of 2)
console.log(Math.LN10); // Output: 2.302585092994046 (Natural logarithm of 10)
console.log(Math.LOG2E); // Output: 1.4426950408889634 (Base 2 logarithm of E)
console.log(Math.LOG10E); // Output: 0.4342944819032518 (Base 10 logarithm of E)

Math.min() and Math.max(): Find the lowest or highest value in a list of arguments.

console.log(Math.min(0, 150, 30, 20, -8, -200)); // Output: -200
console.log(Math.max(0, 150, 30, 20, -8, -200)); // Output: 150
// To find min/max in an array, use the spread operator:
const numbers = [10, 5, 100, 2, 25];
console.log(Math.min(...numbers)); // Output: 2
console.log(Math.max(...numbers)); // Output: 100

Math.abs(x): Returns the absolute (positive) value of x.

console.log(Math.abs(-7.25)); // Output: 7.25

Math.pow(x, y): Returns the value of x to the power of y (xy).

console.log(Math.pow(2, 3)); // Output: 8 (2 * 2 * 2)
console.log(Math.pow(4, 0.5)); // Output: 2 (Square root of 4)

Math.sqrt(x): Returns the square root of x.

console.log(Math.sqrt(9)); // Output: 3

Math.cbrt(x): Returns the cube root of x (ES6+).

console.log(Math.cbrt(27)); // Output: 3

Math.sign(x): Returns the sign of a number, indicating whether x is positive, negative or zero (ES6+). (1 for positive, -1 for negative, 0 for zero)

console.log(Math.sign(10)); // Output: 1
console.log(Math.sign(-5)); // Output: -1
console.log(Math.sign(0)); // Output: 0

Math.round(x): Rounds x to the nearest integer.

console.log(Math.round(4.7)); // Output: 5
console.log(Math.round(4.4)); // Output: 4
console.log(Math.round(4.5)); // Output: 5 (rounds .5 up)

Math.ceil(x): Rounds x up to the nearest integer.

console.log(Math.ceil(4.4)); // Output: 5
console.log(Math.ceil(4.0)); // Output: 4

Math.floor(x): Rounds x down to the nearest integer.

console.log(Math.floor(4.7)); // Output: 4
console.log(Math.floor(4.0)); // Output: 4

Math.trunc(x): Returns the integer part of x by removing any fractional digits (ES6+).

console.log(Math.trunc(4.7)); // Output: 4
console.log(Math.trunc(-4.2)); // Output: -4

Math.random(): Returns a pseudo-random floating-point number between 0 (inclusive) and 1 (exclusive).

console.log(Math.random()); // e.g., 0.123456789
// To get a random integer between min (inclusive) and max (inclusive):
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
console.log(getRandomInt(1, 10)); // Random integer between 1 and 10

The Math object also includes standard trigonometric functions (like Math.sin(), Math.cos(), Math.tan(), Math.asin(), Math.acos(), Math.atan(), Math.atan2()) and logarithmic functions (Math.log() (natural log), Math.log10() (base 10 log, ES6+), Math.log2() (base 2 log, ES6+), Math.exp() (Ex)).

Arguments for trigonometric functions are typically in radians.

console.log(Math.sin(Math.PI / 2)); // Output: 1 (sin(90 degrees))
console.log(Math.log(Math.E)); // Output: 1 (ln(e))
console.log(Math.log10(100)); // Output: 2 (log base 10 of 100)

For a comprehensive reference, including detailed descriptions and examples of all Math properties and methods, consult the MDN Web Docs for the JavaScript Math object.