The math object provides you properties and methods for mathematical constants and functions. Unlike other global objects, math is not a constructor. All the properties and methods of math are static and can be called by using math as an object without creating it.
Math Properties JavaScript provides 8 mathematical constants that can be accessed as Math properties:
The syntax for any Math property is:
Math.property.
Example:
Math.E // Euler's constant and the base of natural logarithms, approximately 2.718
Math.PI // returns PI
Math.SQRT2 // returns the square root of 2, approximately 1.414
Math.SQRT1_2 // returns the square root of 1/2; equivalently, 1 over the square root of 2, approximately 0.707
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:
Example:
Math.round(4.9); // returns 5
Math.round(4.7); // returns 5
Math.round(4.4); // returns 4
Math.round(4.2); // returns 4
Math.round(-4.2); // returns -4
Math.ceil()
Math.ceil(x) returns the value of x rounded up to its nearest integer:
Example:
Math.ceil(4.9); // returns 5
Math.ceil(4.7); // returns 5
Math.ceil(4.4); // returns 5
Math.ceil(4.2); // returns 5
Math.ceil(-4.2); // returns -4
Math.floor()
Math.floor(x) returns the value of x rounded down to its nearest integer:
Example
Math.floor(4.9); // returns 4
Math.floor(4.7); // returns 4
Math.floor(4.4); // returns 4
Math.floor(4.2); // returns 4
Math.floor(-4.2); // returns -5
Math.trunc()
Math.trunc(x) returns the integer part of x:
Example:
Math.trunc(4.9); // returns 4
Math.trunc(4.7); // returns 4
Math.trunc(4.4); // returns 4
Math.trunc(4.2); // returns 4
Math.trunc(-4.2); // returns -4
Math.sign()
Math.sign(x) returns if x is negative, null or positive:
Example:
Math.sign(-4); // returns -1
Math.sign(0); // returns 0
Math.sign(4); // returns 1
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.pow(8, 2); // returns 64
Math.sqrt()
Math.sqrt(x) returns the square root of x:
Example:
Math.sqrt(64); // returns 8
Math.abs()
Math.abs(x) returns the absolute (positive) value of x:
Example:
Math.abs(-4.7); // returns 4.7
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.sin(90 * Math.PI / 180); // returns 1 (the sine of 90 degrees)
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.cos(0 * Math.PI / 180); // returns 1 (the cos of 0 degrees)
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:
Math.min(0, 150, 30, 20, -8, -200); // returns -200
Example:
Math.max(0, 150, 30, 20, -8, -200); // returns 150
Math.random()
Math.random() returns a random number between 0 (inclusive), and 1 (exclusive):
Example:
Math.random(); // returns a random number
The Math.log() Method
Math.log(x) returns the natural logarithm of x:
Example:
Math.log(1); // returns 0
The natural logarithm returns the time needed to reach a certain level of growth.
Math.E and Math.log() are twins.
How many times must we multiply Math.E to get 10?
Example:
Math.log(10); // returns 2.302585092994046
The Math.log2() Method
Math.log2(x) returns the base 2 logarithm of x.
How many times must we multiply 2 to get 8?
Example:
Math.log2(8); // returns 3
The Math.log10() Method
Math.log10(x) returns the base 10 logarithm of x.
How many times must we multiply 10 to get 1000?
Example:
Math.log10(1000); // returns 3
I said earlier on, you cannot consider the Math object as the constructor. It only provides built-in properties and methods for performing mathematical tasks on numbers. To make use of the Math object,You will have to extend the Math object directly instead of using the prototype.
You can read The complete reference to Math Object in JavaScript here