Skip to content

Math

The Math global object is a namespace that groups mathematical constants and functions, such as rounding, powers, logarithms, trigonometry, and random numbers. It can’t be constructed and only holds static members. Every method works on number values and returns a number:

Language: JavaScript
> Math.PI
3.141592653589793
> Math.hypot(3, 4)
5

Math is an ECMAScript built-in that has been part of the language since its first edition. Calling Math() or new Math() throws a TypeError, because Math isn’t a function. Most methods convert their arguments to numbers first, so they throw a TypeError for bigint values.

Key Static Methods and Properties

Member Kind Description
Math.PI, Math.E Static property Holds the constants pi and Euler’s number
Math.round() Static method Returns the nearest integer, rounding halves up toward positive infinity
Math.floor(), Math.ceil() Static method Rounds down or up to an integer
Math.trunc() Static method Removes the fractional part and keeps the integer part
Math.sign() Static method Returns 1, -1, 0, -0, or NaN to show the sign of a number
Math.abs() Static method Returns the absolute value
Math.max(), Math.min() Static method Returns the largest or smallest of its arguments
Math.pow(), Math.sqrt(), Math.cbrt() Static method Raises to a power or returns a square or cube root
Math.hypot() Static method Returns the square root of the sum of the squares of its arguments
Math.log(), Math.log2(), Math.log10() Static method Returns the natural, base-2, or base-10 logarithm
Math.sin(), Math.cos(), Math.atan2() Static method Computes trigonometric functions, with angles in radians
Math.random() Static method Returns a pseudo-random number from 0 up to but not including 1
Math.fround() Static method Rounds to the nearest 32-bit float
Math.f16round() Static method Rounds to the nearest 16-bit float (ES2025)
Math.sumPrecise() Static method Adds the numbers in an iterable without losing precision in intermediate results (ES2026)

Math.f16round() is Baseline Newly available since 2025. Math.sumPrecise() is Baseline Newly available since 2026, but some server runtimes don’t ship it yet.

Examples

A weather app shows a temperature of -4.7 degrees as a whole number. The rounding methods disagree on negative values, because Math.floor() moves toward negative infinity while Math.trunc() moves toward zero:

Language: JavaScript
> Math.round(-4.7)
-5
> Math.floor(-4.7)
-5
> Math.trunc(-4.7)
-4

Drag the value along the number line from -3 to 3, including negative values like -2.7 and halves like -2.5 and 2.5, to see Math.round(), Math.ceil(), Math.floor(), and Math.trunc() land in different places:

Interactive diagram — enable JavaScript to view.

A board game needs a die roll from one to six. Scaling Math.random() and flooring the result gives an integer in that range:

Language: JavaScript
> const roll = Math.floor(Math.random() * 6) + 1;
undefined

> Number.isInteger(roll) && roll >= 1 && roll <= 6
true

Math.max() takes separate arguments, so spread syntax passes an array of readings to it:

Language: JavaScript
> const readings = [18.5, 22.1, 19.8];
undefined

> Math.max(...readings)
22.1
> Math.min(...readings)
18.5

A ledger that sums very large and very small amounts loses the small ones with +. The following js norun block uses Math.sumPrecise(), which not every JavaScript runtime ships yet:

Language: JavaScript
const entries = [1e20, 0.1, -1e20];
entries.reduce((total, n) => total + n, 0);
Math.sumPrecise(entries);

The .reduce() call returns 0 because 0.1 vanishes when it’s added to 1e20. The Math.sumPrecise() call returns 0.1, the exact sum rounded once at the end.

Common Gotchas

Math.round() rounds halves toward positive infinity, so a negative half rounds up instead of away from zero:

Language: JavaScript
> Math.round(2.5)
3
> Math.round(-2.5)
-2

Spreading an empty array into Math.max() returns -Infinity, and Math.min() returns Infinity, instead of throwing an error:

Language: JavaScript
> Math.max(...[])
-Infinity
> Math.min(...[])
Infinity

Math.random() isn’t suitable for passwords or tokens, because its output isn’t cryptographically secure. The host-provided crypto.getRandomValues() function fills that role.

Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.


By Leodanis Pozo Ramos • Updated Nov. 7, 2026