Skip to content

JavaScript Math API

JavaScript Math object — constants and functions for numbers.

1 class · 9 methods

Math

9 methods

Built-in object with mathematical constants and functions.

Math.max(...values)

Return the largest of zero or more numbers. Returns -Infinity with no args.

Parameters

NameTypeDescription
values...numberNumbers to compare.

Returns

number

Example

javascript
Math.max(1, 2, 3)             // 3
Math.max(...[1, 2, 3])        // 3
Math.max()                    // -Infinity
Math.min(...values)

Return the smallest of zero or more numbers. Returns Infinity with no args.

Parameters

NameTypeDescription
values...numberNumbers to compare.

Returns

number

Example

javascript
Math.min(1, 2, 3)             // 1
Math.min(...[1, 2, 3])        // 1
Math.min()                    // Infinity
Math.round(x)

Return the value of x rounded to the nearest integer.

Parameters

NameTypeDescription
xnumberNumber to round.

Returns

number

Example

javascript
Math.round(2.4)  // 2
Math.round(2.5)  // 3
Math.round(-2.5) // -2 (rounds toward +Infinity)
Math.floor(x)

Return the largest integer less than or equal to x.

Parameters

NameTypeDescription
xnumberNumber to floor.

Returns

number

Example

javascript
Math.floor(2.7)   // 2
Math.floor(-2.3)  // -3
Math.ceil(x)

Return the smallest integer greater than or equal to x.

Parameters

NameTypeDescription
xnumberNumber to ceil.

Returns

number

Example

javascript
Math.ceil(2.1)   // 3
Math.ceil(-2.9)  // -2
Math.abs(x)

Return the absolute value of x.

Parameters

NameTypeDescription
xnumberNumber.

Returns

number

Example

javascript
Math.abs(-5)    // 5
Math.abs(3.14)  // 3.14
Math.abs(-7.5)  // 7.5
Math.pow(base, exponent)

Return base raised to the exponent power (equivalent to base ** exponent).

Parameters

NameTypeDescription
basenumberBase.
exponentnumberExponent.

Returns

number

Example

javascript
Math.pow(2, 10)   // 1024
Math.pow(9, 0.5)  // 3
2 ** 10           // 1024
Math.sqrt(x)

Return the positive square root of x. Returns NaN if x < 0.

Parameters

NameTypeDescription
xnumberNon-negative number.

Returns

number

Example

javascript
Math.sqrt(9)   // 3
Math.sqrt(2)   // 1.4142135623730951
Math.sqrt(-1)  // NaN
Math.random()

Return a pseudo-random number in [0, 1).

Returns

number

Example

javascript
Math.random()                       // e.g. 0.4246...
Math.floor(Math.random() * 10)       // 0..9
Math.floor(Math.random() * 6) + 1    // 1..6 (dice)