Back to: JavaScript
Math is a JavaScript object that allows you to perform mathematical tasks on numbers.
The syntax for all Math properties is:
Math.property
8 of the properties are constants:
Math.PI
The Math.PI property returns the value of pi, which is the ratio of the circumference of a circle to its diameter. Math.PI returns the value of pi to 15 decimal places: 3.1 4 1 5 9 2 6 5 3 5 8 9 7 9 3
Math.SQRT1_2
Math.SQRT2
Math.E
Math.LN2
Math.LN10
Math.LOG2E
Math.LOG10E
There are 4 ways of rounding numbers:
Math.round(x)
Math.ceil(x)
Math.floor(x)
Math.trunc(x)
If you need a random number:
Math.random()
Math.cbrt(x)
Math.cos(x)
The Math.cos method returns the cosine of a number.
Syntax:
Math.cos(x)
x is in radians. The Math.cos() method returns a value between -1 and 1.
Math.cosh(x)
Math.random()
Math.round(x)
Math.sign(x)
Math.sin(x)
The Math.sin function returns the sine of a number.
Syntax:
Math.sin(x)
x is in radians. The Math.sin() mathod returns a value between -1 and 1.
Math.sinh(x)
Math.sqrt(x)
Math.tan(x)
Math.tanh(x)
There are 5 methods that were added with ES6:
Math.trunc()
Math.sign()
Math.cbrt()
Math.log2()
Math.log10()
NOTE:
The precision of some Math functions is implementation-dependent!