- JavaScript Math Reference
- Example
- Math Tutorial
- Math Object Methods and Properties
- Math
- Description
- Static properties
- Static methods
- Examples
- Converting between degrees and radians
- Calculating the height of an equilateral triangle
- Returning a random integer between two bounds
- Specifications
- Browser compatibility
- See also
- Found a content problem with this page?
- MDN
- Support
- Our communities
- Developers
- JavaScript Math Object
- Example
- The Math Object
- Math Properties (Constants)
- Example
- Math Methods
- Number to Integer
- Math.round()
- Examples
- Math.ceil()
- Example
- Math.floor()
- Example
- Math.trunc()
- Example
- Math.sign()
- Example
- Math.pow()
- Example
- Math.sqrt()
- Example
- Math.abs()
- Example
- Math.sin()
- Example
- Math.cos()
- Example
- Math.min() and Math.max()
- Example
- Example
- Math.random()
- Example
- The Math.log() Method
- Examples
- The Math.log2() Method
- The Math.log10() Method
- JavaScript Math Methods
- Complete Math Reference
JavaScript Math Reference
The Math object allows you to perform mathematical tasks.
Math is not a constructor. All properties/methods of Math can be called by using Math as an object, without creating it:
Example
Math Tutorial
Math Object Methods and Properties
Name | 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 |
clz32(x) | Returns the number of leading zeros in a 32-bit binary representation of x |
cos(x) | Returns the cosine of x (x is in radians) |
cosh(x) | Returns the hyperbolic cosine of x |
E | Returns Euler’s number (approx. 2.718) |
exp(x) | Returns the value of E x |
expm1(x) | Returns the value of E x minus 1 |
floor(x) | Returns x, rounded downwards to the nearest integer |
fround(x) | Returns the nearest (32-bit single precision) float representation of a number |
LN2 | Returns the natural logarithm of 2 (approx. 0.693) |
LN10 | Returns the natural logarithm of 10 (approx. 2.302) |
log(x) | Returns the natural logarithmof x |
log10(x) | Returns the base-10 logarithm of x |
LOG10E | Returns the base-10 logarithm of E (approx. 0.434) |
log1p(x) | Returns the natural logarithm of 1 + x |
log2(x) | Returns the base-2 logarithm of x |
LOG2E | Returns the base-2 logarithm of E (approx. 1.442) |
max(x1,x2. ) | Returns the number with the highest value |
min(x1,x2. ) | Returns the number with the lowest value |
PI | Returns PI (approx. 3.14) |
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 the sign of a number (checks whether it is positive, negative or zero) |
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 |
SQRT1_2 | Returns the square root of 1/2 (approx. 0.707) |
SQRT2 | Returns the square root of 2 (approx. 1.414) |
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) |
Math
The Math namespace object contains static properties and methods for mathematical constants and functions.
Math works with the Number type. It doesn’t work with BigInt .
Description
Unlike most global objects, Math is not a constructor. You cannot use it with the new operator or invoke the Atomics object as a function. All properties and methods of Math are static.
Note: Many Math functions have a precision that’s implementation-dependent.
This means that different browsers can give a different result. Even the same JavaScript engine on a different OS or architecture can give different results!
Static properties
Euler’s number and the base of natural logarithms; approximately 2.718 .
Natural logarithm of 10 ; approximately 2.303 .
Natural logarithm of 2 ; approximately 0.693 .
Base-10 logarithm of E ; approximately 0.434 .
Base-2 logarithm of E ; approximately 1.443 .
Ratio of a circle’s circumference to its diameter; approximately 3.14159 .
Square root of ½; approximately 0.707 .
Square root of 2 ; approximately 1.414 .
The initial value of the @@toStringTag property is the string «Math» . This property is used in Object.prototype.toString() .
Static methods
Returns the absolute value of x .
Returns the arccosine of x .
Returns the hyperbolic arccosine of x .
Returns the hyperbolic arcsine of a number.
Returns the arctangent of x .
Returns the arctangent of the quotient of its arguments.
Returns the hyperbolic arctangent of x .
Returns the cube root of x .
Returns the smallest integer greater than or equal to x .
Returns the number of leading zero bits of the 32-bit integer x .
Returns the hyperbolic cosine of x .
Returns e x , where x is the argument, and e is Euler’s number ( 2.718 …, the base of the natural logarithm).
Returns subtracting 1 from exp(x) .
Returns the largest integer less than or equal to x .
Returns the nearest single precision float representation of x .
Returns the square root of the sum of squares of its arguments.
Returns the result of the 32-bit integer multiplication of x and y .
Returns the natural logarithm (㏒e; also, ㏑) of x .
Returns the base-10 logarithm of x .
Returns the natural logarithm (㏒e; also ㏑) of 1 + x for the number x .
Returns the base-2 logarithm of x .
Returns the largest of zero or more numbers.
Returns the smallest of zero or more numbers.
Returns base x to the exponent power y (that is, x y ).
Returns a pseudo-random number between 0 and 1 .
Returns the value of the number x rounded to the nearest integer.
Returns the sign of the x , indicating whether x is positive, negative, or zero.
Returns the hyperbolic sine of x .
Returns the positive square root of x .
Returns the hyperbolic tangent of x .
Returns the integer portion of x , removing any fractional digits.
Examples
Converting between degrees and radians
The trigonometric functions sin() , cos() , tan() , asin() , acos() , atan() , and atan2() expect (and return) angles in radians.
Since humans tend to think in degrees, and some functions (such as CSS transforms) can accept degrees, it is a good idea to keep functions handy that convert between the two:
function degToRad(degrees) return degrees * (Math.PI / 180); > function radToDeg(rad) return rad / (Math.PI / 180); >
Calculating the height of an equilateral triangle
If we want to calculate the height of an equilateral triangle, and we know its side length is 100, we can use the formulae length of the adjacent multiplied by the tangent of the angle is equal to the opposite.
In JavaScript, we can do this with the following:
We use our degToRad() function to convert 60 degrees to radians, as Math.tan() expects an input value in radians.
Returning a random integer between two bounds
This can be achieved with a combination of Math.random() and Math.floor() :
function random(min, max) const num = Math.floor(Math.random() * (max - min + 1)) + min; return num; > random(1, 10);
Specifications
Browser compatibility
BCD tables only load in the browser
See also
Found a content problem with this page?
This page was last modified on Apr 26, 2023 by MDN contributors.
Your blueprint for a better internet.
MDN
Support
Our communities
Developers
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998– 2023 by individual mozilla.org contributors. Content available under a Creative Commons license.
JavaScript Math Object
The JavaScript Math object allows you to perform mathematical tasks on numbers.
Example
The Math Object
Unlike other objects, the Math object has no constructor.
The Math object is static.
All methods and properties can be used without creating a Math object first.
Math Properties (Constants)
The syntax for any Math property is : Math.property .
JavaScript provides 8 mathematical constants that can be accessed as Math properties:
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
Math Methods
The syntax for Math any methods is : 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()
Math.round(x) returns the nearest integer:
Examples
Math.ceil()
Math.ceil(x) returns the value of x rounded up to its nearest integer:
Example
Math.floor()
Math.floor(x) returns the value of x rounded down to its nearest integer:
Example
Math.trunc()
Math.trunc(x) returns the integer part of x:
Example
Math.sign()
Math.sign(x) returns if x is negative, null or positive:
Example
Math.trunc() and Math.sign() were added to JavaScript 2015 — ES6.
Math.pow()
Math.pow(x, y) returns the value of x to the power of y:
Example
Math.sqrt()
Math.sqrt(x) returns the square root of x:
Example
Math.abs()
Math.abs(x) returns the absolute (positive) value of x:
Example
Math.sin()
Math.sin(x) returns the sine (a value between -1 and 1) of the angle x (given in radians).
If you want to use degrees instead of radians, you have to convert degrees to radians:
Angle in radians = Angle in degrees x PI / 180.
Example
Math.cos()
Math.cos(x) returns the cosine (a value between -1 and 1) of the angle x (given in radians).
If you want to use degrees instead of radians, you have to convert degrees to radians:
Angle in radians = Angle in degrees x PI / 180.
Example
Math.min() and Math.max()
Math.min() and Math.max() can be used to find the lowest or highest value in a list of arguments:
Example
Example
Math.random()
Math.random() returns a random number between 0 (inclusive), and 1 (exclusive):
Example
You will learn more about Math.random() in the next chapter of this tutorial.
The Math.log() Method
Math.log(x) returns the natural logarithm of x.
The natural logarithm returns the time needed to reach a certain level of growth:
Examples
Math.E and Math.log() are twins.
How many times must we multiply Math.E to get 10?
The Math.log2() Method
Math.log2(x) returns the base 2 logarithm of x.
How many times must we multiply 2 to get 8?
The Math.log10() Method
Math.log10(x) returns the base 10 logarithm of x.
How many times must we multiply 10 to get 1000?
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 E x |
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) |
Complete Math Reference
For a complete reference, go to our Complete Math Object Reference.
The reference contains descriptions and examples of all Math properties and methods.