The JavaScript Math object enables you to perform mathematical operations with numbers.
Example
Math.PI; |
Unlike many other objects in JavaScript, the Math object does not have a constructor and is static.
This means all its methods and properties can be accessed directly without needing to create an instance of the Math object.
JavaScript offers 8 mathematical constants accessible through Math properties, using the syntax: Math.property.
Example
Math.E // returns Euler’s number Math.PI // returns PI Math.SQRT2 // returns the square root of 2 Math.SQRT1_2 // returns the square root of 1/2 Math.LN2 // returns the natural logarithm of 2 Math.LN10 // returns the natural logarithm of 10 Math.LOG2E // returns base 2 logarithm of E Math.LOG10E // returns base 10 logarithm of E |
The syntax for any Math method is: Math.method(number).
There are four commonly used methods to round a number to an integer.
Math.round(x) |
Returns x rounded to the nearest integer.
|
Math.ceil(x) |
Returns x rounded up to the nearest integer.
|
Math.floor(x) |
Returns x rounded down to the nearest integer.
|
Math.trunc(x) |
Returns the integer part of x (introduced in ES6).
|
Math.round(x) calculates and returns the nearest integer to x.
Example
Math.round(4.6); |
Math.round(4.5); |
Math.round(4.4); |
Math.ceil(x) returns the smallest integer greater than or equal to x.
Example
Math.ceil(4.9); Math.ceil(4.7); Math.ceil(4.4); Math.ceil(4.2); Math.ceil(-4.2); |
Math.floor(x) returns the largest integer less than or equal to x.
Example
Math.floor(4.9); Math.floor(4.7); Math.floor(4.4); Math.floor(4.2); Math.floor(-4.2); |
Math.trunc(x) returns the integer part of x by removing any fractional digits.
Example
Math.trunc(4.9); Math.trunc(4.7); Math.trunc(4.4); Math.trunc(4.2); Math.trunc(-4.2); |
Math.sign(x) returns -1 if x is negative, 0 if x is zero, and 1 if x is positive.
Example
Math.sign(-4); Math.sign(0); Math.sign(4); |
Math.pow(x, y) calculates and returns the value of x raised to the power of y.
Example
Math.pow(8, 2); |
Math.sqrt(x) computes and returns the square root of x.
Example
Math.sqrt(64); |
Math.abs(x) returns the absolute value of x, which is always non-negative.
Example
Math.abs(-4.7); |
Math.sin(x) returns the sine of the angle x, where x is given in radians and the result is between -1 and 1.
To use degrees instead of radians, convert degrees to radians using the formula:
Angle in radians = Angle in degrees × PI / 180.
Example
Math.sin(90 * Math.PI / 180); // returns 1 (the sine of 90 degrees) |
Math.cos(x) returns the cosine of the angle x, where x is given in radians and the result is between -1 and 1.
To use degrees instead of radians, convert degrees to radians using this formula:
Angle in radians = Angle in degrees × PI / 180.
Example
Math.cos(0 * Math.PI / 180); // returns 1 (the cos of 0 degrees) |
Math.min() and Math.max() are used to determine the smallest or largest value among a set of arguments.
Example
Math.min(0, 150, 30, 20, –8, –200); |
Example
Math.max(0, 150, 30, 20, –8, –200); |
Math.random() generates and returns a random number between 0 (inclusive) and 1 (exclusive).
Example
Math.random(); |
In the next chapter of this tutorial, you will delve deeper into Math.random(). |
Math.log(x) returns the natural logarithm of x.
The natural logarithm represents the time required to reach a certain level of growth.
Example
Math.log(1); |
Math.log(2); |
Math.log(3); |
Math.E and Math.log() are closely related.
How many times must we multiply Math.E to reach 10?
Math.log(10); |
Math.log2(x) calculates and returns the logarithm base 2 of x.
To reach 8, how many times must we multiply 2?
Math.log2(8); |
Math.log10(x) computes and returns the logarithm base 10 of x.
To reach 1000, how many times must we multiply 10?
Math.log10(1000); |
Method |
Description |
abs(x) |
Bring back the absolute value of x. |
acos(x) |
Bring back the arccosine of x, in radians |
acosh(x) |
Bring back the hyperbolic arccosine of x |
asin(x) |
Bring back the arcsine of x, in radians |
asinh(x) |
Bring back the hyperbolic arcsine of x |
atan(x) |
Bring back the arctangent of x as a numeric value between -PI/2 and PI/2 radians |
atan2(y, x) |
Bring back the arctangent of the quotient of its arguments |
atanh(x) |
Bring back the hyperbolic arctangent of x |
cbrt(x) |
Bring back the cubic root of x |
ceil(x) |
Bring back x, rounded upwards to the nearest integer |
cos(x) |
Bring back the cosine of x (x is in radians) |
cosh(x) |
Bring back the hyperbolic cosine of x |
exp(x) |
Bring back the value of Ex |
floor(x) |
Bring back x, rounded downwards to the nearest integer |
log(x) |
Bring back the natural logarithm (base E) of x |
max(x, y, z, …, n) |
Bring back the number with the highest value |
min(x, y, z, …, n) |
Bring back the number with the lowest value |
pow(x, y) |
Bring back the value of x to the power of y |
random() |
Bring back a random number between 0 and 1 |
round(x) |
Bring back x to the nearest integer |
sign(x) |
Bring back if x is negative, null or positive (-1, 0, 1) |
sin(x) |
Bring back the sine of x (x is in radians) |
sinh(x) |
Bring back the hyperbolic sine of x |
sqrt(x) |
Bring back the square root of x |
tan(x) |
Bring back the tangent of an angle |
tanh(x) |
Bring back the hyperbolic tangent of a number |
trunc(x) |
Bring back the integer part of a number (x) |