JAVASCRIPT MATH OBJECT
The JavaScript Math object is used to perform mathematic operations on numbers.
The Math object has no constructor and is a static object so all its methods and properties can be used without a Math object being created.
Math Object Properties/Constants
There are a total of 8 Math constants that are accessed as properties of the Math object.
Math.PI; // returns PI, 3.141592653589793
Math.E; // returns Euler's number, 2.718281828459045
Math.SQRT2; // returns square root of 2, 1.4142135623730951
Math.SQRT1_2; // returns square root of 1/2, 0.7071067811865476
Math.LN2; // returns natural log of 2, 0.6931471805599453
Math.LN10; // returns natural log of 10, 2.302585092994046
Math.LOG2E; // returns base 2 log of E, 1.4426950408889634
Math.LOG10E; // returns base 10 log of E, 0.4342944819032518
Math Methods
Math.round()
The Math.round(num) method rounds the number to the nearest integer.
Math.round(7.9); // returns 8
Math.round(7.5); // returns 8
Math.round(7.4); // returns 7
Live Demo!
Math.ceil()
The Math.ceil(num) method is used to round up the number to the nearest integer.
Math.ceil(7.9); // returns 8
Math.ceil(7.5); // returns 8
Math.ceil(7.4); // returns 8
Math.ceil(-7.4); // returns -7
Live Demo!
Math.floor()
The Math.round(num) method is used to round down the number to the nearest integer.
Math.floor(7.9); // returns 7
Math.floor(7.5); // returns 7
Math.floor(7.4); // returns 7
Math.floor(-7.4); // returns -8
Live Demo!
Math.trunc()
The Math.trunc(num) method is used to return only the integer part of a number.
Math.trunc(7.9); // returns 7
Math.trunc(7.5); // returns 7
Math.trunc(7.4); // returns 7
Math.trunc(-7.4); // returns -7
Live Demo!
Math.sign()
The Math.sign(num) method is used to return if the number is negative, positive or null.
Math.sign(-7); // returns -1
Math.sign(0); // returns 0
Math.sign(7); // returns 1
Live Demo!
Math.pow()
The Math.pow(x, y) is used to return the result of x to the power of y.
Math.pow(7, 2); //Returns 49
Live Demo!
Math.sqrt()
The Math.sqrt(num) methods is used to return the result of the square root of the number.
Math.sqrt(49); //Returns 7
Live Demo!
Math.abs()
The Math.abs(num) method is used to return the absolute/positive value of the number.
Math.abs(-7); //Returns 7
Live Demo!
Math.sin()
The Math.sin(num) method is used to return the sine of an angle that is given in radians.
Math.sin(0.5 * Math.PI); //Returns 1
Live Demo!
Math.cos()
The Math.cos(num) method is used to return the cosine of an angle that is given in radians.
Math.cos(1 * Math.PI); //Returns -1
Live Demo!
Math.min() and Math.max()
The Math.min and Math.max() methods are used to return the maximum or minimum value in the provided arguments.
Math.min(55, 15, 12, 30, -82, -19); //Returns -82
Math.max(55, 15, 12, 30, -82, -19); //Returns 55
Live Demo!
Math.random()
The Math.random() method is used to return a random number from 0 to 1 (1 not included).
Math.random(); //Returns a random number from 0 -> 1 (1 excluded)
Live Demo!
Math.log()
The Math.log(num) method returns the natural log of the provided number.
Math.log(1); //Returns 0
Live Demo!
Math.log2()
The Math.log2(num) method returns the base 2 log of the provided number.
Math.log2(16); //Returns 4
Live Demo!
Math.log10()
The Math.log10(num) method is used to return the base 10 log of the provided number.
Math.log10(100000); // returns 5
Live Demo!