Skip to content

ES6 - Math

The built-in Math object in JavaScript provides properties and methods for mathematical constants and functions. Math is not a constructor; all its properties and methods are static. You call them directly on the Math object itself (e.g., Math.PI, Math.sqrt(x)).

These are common mathematical constants:

PropertyDescription
Math.EEuler’s constant, the base of natural logarithms (approx. 2.718).
Math.LN2Natural logarithm of 2 (approx. 0.693).
Math.LN10Natural logarithm of 10 (approx. 2.302).
Math.LOG2EBase-2 logarithm of E (approx. 1.442).
Math.LOG10EBase-10 logarithm of E (approx. 0.434).
Math.PIRatio of a circle’s circumference to its diameter (approx. 3.14159).
Math.SQRT1_2Square root of 1/2 (approx. 0.707).
Math.SQRT2Square root of 2 (approx. 1.414).

Functions for exponentiation and roots:

FunctionDescription
Math.pow(x, y)Returns x raised to the power y.
Math.sqrt(x)Returns the positive square root of x.
Math.cbrt(x) (ES6)Returns the cube root of x.
Math.exp(x)Returns e^x, where e is Euler’s constant (Math.E).
Math.expm1(x) (ES6)Returns e^x - 1. More accurate than Math.exp(x) - 1 for small x.
Math.hypot(...values) (ES6)Returns the square root of the sum of squares of its arguments (e.g., Math.hypot(3, 4) returns 5).

Functions for calculating logarithms. In JavaScript, Math.log() refers to the natural logarithm.

FunctionDescription
Math.log(x)Returns the natural logarithm (base e) of x.
Math.log10(x) (ES6)Returns the base-10 logarithm of x.
Math.log2(x) (ES6)Returns the base-2 logarithm of x.
Math.log1p(x) (ES6)Returns the natural logarithm of 1 + x. More accurate than Math.log(1 + x) for small x.

Functions for rounding numbers and determining their sign:

FunctionDescription
Math.abs(x)Returns the absolute (non-negative) value of x.
Math.sign(x) (ES6)Returns the sign of x: -1 for negative, +1 for positive, 0 for 0, -0 for -0, NaN for NaN.
Math.ceil(x)Returns the smallest integer greater than or equal to x (rounds up).
Math.floor(x)Returns the largest integer less than or equal to x (rounds down).
Math.round(x)Returns x rounded to the nearest integer (0.5 rounds up).
Math.trunc(x) (ES6)Returns the integer part of x by removing fractional digits (does not round).
Math.min(...values)Returns the smallest of zero or more numbers.
Math.max(...values)Returns the largest of zero or more numbers.

Standard trigonometric functions. All angles are in radians.

FunctionDescription
Math.sin(x)Sine of x (in radians).
Math.cos(x)Cosine of x (in radians).
Math.tan(x)Tangent of x (in radians).
Math.asin(x)Arcsine (inverse sine) of x; result in radians.
Math.acos(x)Arccosine (inverse cosine) of x; result in radians.
Math.atan(x)Arctangent (inverse tangent) of x; result in radians.
Math.atan2(y, x)Arctangent of the quotient y/x; result in radians, considering the signs of y and x to determine the quadrant.
Math.sinh(x) (ES6)Hyperbolic sine of x.
Math.cosh(x) (ES6)Hyperbolic cosine of x.
Math.tanh(x) (ES6)Hyperbolic tangent of x.
Math.asinh(x) (ES6)Inverse hyperbolic sine of x.
Math.acosh(x) (ES6)Inverse hyperbolic cosine of x.
Math.atanh(x) (ES6)Inverse hyperbolic tangent of x.

The Math.random() function returns a floating-point, pseudo-random number in the range 0 (inclusive) up to but not including 1 (exclusive).

// Random number between 0 (inclusive) and 1 (exclusive)
const randomNumber01 = Math.random();
console.log(`Random number [0, 1): ${randomNumber01}`);
// 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;
}
const randomDieRoll = getRandomInt(1, 6);
console.log(`Random die roll (1-6): ${randomDieRoll}`);
const randomLargeInt = getRandomInt(50, 100);
console.log(`Random integer (50-100): ${randomLargeInt}`);
Random number [0, 1): 0.xxxxxxxxxxxxxxx
Random die roll (1-6): (a number between 1 and 6)
Random integer (50-100): (a number between 50 and 100)

For cryptographically secure random numbers, Math.random() is not suitable. Use the Crypto.getRandomValues() method from the Web Crypto API instead.