Math.E // returns Euler's number
Math.PI // returns PI - 3.14...
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
Properties of the Math object.
Math.round(4.5) // 5
Math.round(4.4) // 4
Returns the value of a number rounded to the nearest integer.
Math.floor(4.5) // 4
Math.floor(4.4) // 4
Returns and always rounds down to the nearest integer.
Math.log(10); // 2.302585092994046
Math.log2(1024); // 10
Math.log10(100000); // 5
Returns the natural logarithm (base e) of a number.
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 integer part of x
Math.sign(x) // returns if x is negative, null or positive (-1, 0, 1)
Math.pow(x, y) // returns the value of x to the power of y:
Math.sqrt(x) // returns the square root of x:
Math.abs(x) // returns the absolute (positive) value of x:
Math.sin(x) // returns the sine (a value between -1 and 1) of the angle x (given in radians)
Math.cos(x) // returns the cosine (a value between -1 and 1) of the angle x (given in radians)
Math.min() // used to find the lowest value
Math.max() // used to find the highest value in a list of arguments
Math.random() // returns a random number between 0 (inclusive), and 1 (exclusive)
Math.log(x) // returns the natural logarithm of x
Math.log2(x) // returns the base 2 logarithm of x
Math.log10(x) // returns the base 10 logarithm of x
Methods of the Javascript Math Object.
Math.floor(Math.random() * 3); // Returns a random integer, 0, 1, or 2
Math.floor(Math.random() * 3) + 1; // Returns a random integer, 1, 2, or 3
function getRandomNum(min, max) {
return Math.floor(Math.random() * (max - min) ) + min; // Returns random number (max not included)
}
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1) ) + min; // Returns a random number (max included)
}
Math.random() returns a random number between 0 & 1.
Math.ceil(4.5) // 5
Math.ceil(4.4) // 5
Returns and always rounds up to its nearest integer.
Math.sign(3) // 1
Math.sign(0) // 0
Math.sign(-3) // -1
Returns if value is positive, negative, or null.