JavaScript Math Object
JavaScript Math Object
w3schools.com LOG IN
The JavaScript Math object allows you to perform mathematical tasks on numbers.
Example
Math.PI; // returns 3.141592653589793
Try it Yourself »
Math.round()
Math.round(x) returns the value of x rounded to its nearest integer:
Example
Math.round(4.7); // returns 5
Math.round(4.4); // returns 4
https://www.w3schools.com/js/js_math.asp 1/13
1/16/2021 JavaScript Math Object
Try it Yourself »
Math.pow()
Math.pow(x, y) returns the value of x to the power of y:
Example
Try it Yourself »
Math.sqrt()
Math.sqrt(x) returns the square root of x:
Example
Math.sqrt(64); // returns 8
Try it Yourself »
https://www.w3schools.com/js/js_math.asp 2/13
1/16/2021 JavaScript Math Object
Math.abs()
Math.abs(x) returns the absolute (positive) value of x:
Example
Try it Yourself »
Math.ceil()
Math.ceil(x) returns the value of x rounded up to its nearest integer:
Example
Math.ceil(4.4); // returns 5
Try it Yourself »
Math.floor()
Math.floor(x) returns the value of x rounded down to its nearest integer:
Example
Math.floor(4.7); // returns 4
https://www.w3schools.com/js/js_math.asp 3/13
1/16/2021 JavaScript Math Object
Try it Yourself »
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:
Example
Math.sin(90 * Math.PI / 180); // returns 1 (the sine of 90 degrees)
Try it Yourself »
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:
Example
Math.cos(0 * Math.PI / 180); // returns 1 (the cos of 0 degrees)
Try it Yourself »
https://www.w3schools.com/js/js_math.asp 4/13
1/16/2021 JavaScript Math Object
Example
Math.min(0, 150, 30, 20, -8, -200); // returns -200
Try it Yourself »
Example
Math.max(0, 150, 30, 20, -8, -200); // returns 150
Try it Yourself »
Math.random()
Math.random() returns a random number between 0 (inclusive), and 1 (exclusive):
Example
Math.random(); // returns a random number
Try it Yourself »
You will learn more about Math.random() in the next chapter of this tutorial.
https://www.w3schools.com/js/js_math.asp 5/13
1/16/2021 JavaScript Math Object
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
Try it Yourself »
Math Constructor
Unlike other global objects, the Math object has no constructor. Methods and properties
are static.
All methods and properties (constants) can be used without creating a Math object first.
https://www.w3schools.com/js/js_math.asp 6/13
1/16/2021 JavaScript Math Object
https://www.w3schools.com/js/js_math.asp 7/13