JavaScript Math Object


The Math Object

Unlike some objects, the Math object doesn't need a constructor.

The Math object is static.

All the functions and attributes in Math can be employed without the need to create a Math object beforehand.

Math Properties (Constants)

The way you write any Math property in JavaScript is like this: Math.property.

JavaScript gives you access to 8 special mathematical values through the Math object properties:


Math Methods

The way you use math methods in JavaScript is like this: Math.method(number).

Number to Integer

There are 4 common methods to round a number to an integer:

Math.round(x)Returns x rounded to its nearest integer
Math.ceil(x)Returns x rounded up to its nearest integer
Math.floor(x)Returns x rounded down to its nearest integer
Math.trunc(x)Returns the integer part of x (new in ES6)

Math.round()

The Math.round(x) function gives you the closest whole number to the value of x.


Math.ceil()

The code Math.ceil(x) gives you the nearest whole number that's equal to or greater than the value of x.


Math.floor()

The Math.floor(x) function gives you the whole number that's equal to or less than the number x. The code rounds a number down to the whole number it is closest to.


Math.trunc()

The Math.trunc(x) function gives you the whole number part of the value x:


Math.sign()

The Math.sign(x) function tells you whether the number 'x' is negative, zero, or positive.

Math.trunc() and Math.sign() were added to JavaScript 2015 - ES6.


Math.pow()

The Math.pow(x, y) function calculates x raised to the power of y.


Math.sqrt()

The code snippet Math.sqrt(x) calculates the square root of the number x.


Math.abs()

The code Math.abs(x) gives you the positive value of the number x.


Math.sin()

The code Math.sin(x) calculates the sine (a number between -1 and 1) of an angle represented by x in radians.

If you prefer to work with degrees instead of radians, you'll need to convert degrees into radians.

The angle in radians equals the angle in degrees multiplied by PI divided by 180.


Math.cos()

The code Math.cos(x) gives you the cosine of an angle (measured in radians), which is a number between -1 and 1.

If you prefer to work with degrees rather than radians, you'll need to convert degrees into radians:

Angle in radians = Angle in degrees x PI / 180.


Math.min() and Math.max()

You can use Math.min() to find the smallest value in a list of numbers, and Math.max() to find the largest value.


Math.random()

The code Math.random() gives you a random number between 0 (including 0) and 1 (not including 1).

You will learn more about Math.random() in the next chapter of this tutorial.


The Math.log() Method

Math.log(x) gives you the natural logarithm of the number x.

The natural logarithm helps determine the time it takes to achieve a specific level of growth.

Math.E and Math.log() are twins.


The Math.log2() Method

The code snippet Math.log2(x) calculates the logarithm of x with a base of 2.


The Math.log10() Method

The code Math.log10(x) calculates the base 10 logarithm of the number x.


JavaScript Math Methods

Method Description
abs(x) Returns the absolute value of x
acos(x) Returns the arccosine of x, in radians
acosh(x) Returns the hyperbolic arccosine of x
asin(x) Returns the arcsine of x, in radians
asinh(x) Returns the hyperbolic arcsine of x
atan(x) Returns the arctangent of x as a numeric value between -PI/2 and PI/2 radians
atan2(y, x) Returns the arctangent of the quotient of its arguments
atanh(x) Returns the hyperbolic arctangent of x
cbrt(x) Returns the cubic root of x
ceil(x) Returns x, rounded upwards to the nearest integer
cos(x) Returns the cosine of x (x is in radians)
cosh(x) Returns the hyperbolic cosine of x
exp(x) Returns the value of Ex
floor(x) Returns x, rounded downwards to the nearest integer
log(x) Returns the natural logarithm (base E) of x
max(x, y, z, ..., n) Returns the number with the highest value
min(x, y, z, ..., n) Returns the number with the lowest value
pow(x, y) Returns the value of x to the power of y
random() Returns a random number between 0 and 1
round(x) Rounds x to the nearest integer
sign(x) Returns if x is negative, null or positive (-1, 0, 1)
sin(x) Returns the sine of x (x is in radians)
sinh(x) Returns the hyperbolic sine of x
sqrt(x) Returns the square root of x
tan(x) Returns the tangent of an angle
tanh(x) Returns the hyperbolic tangent of a number
trunc(x) Returns the integer part of a number (x)