The JavaScript Math object used to perform mathematical tasks with numbers.Math
is an object that has properties and methods.
Math
has no constructor. All Math’s
properties and methods are static.
To refer methods and properties use Math.propertName and functions as Math.funcationName().
For example to access the constant pi use Math.PI
To
call the sine function use Math.sin(valuex)
, where valuex
is a argument. Constants are defined with the full precision of real numbers.
The math object provide properties and methods of constants and functions for mathematical.
Math’s properties and methods can be called by using Math as an object without creating it.
Syntax
1 2 | Math.propertyName Math.functionName() |
Example
1 2 | Math.PI Math.sin(50); |
1 2 3 4 5 6 7 8 9 | <html> <body> <p id="piValue"></p> <script> document.getElementById("piValue").innerHTML = Math.PI; </script> </body> </html> |
Math Properties (Constants)
Property | Description |
---|---|
E | It will return Euler’s number (approx. 2.718). |
LN2 | It will return tde natural logaritdm of 2 (approx. 0.693) |
LN10 | It will return tde natural logaritdm of 10 (approx. 2.302) |
LOG2E | It will return tde base-2 logaritdm of E (approx. 1.442) |
LOG10E | It will return tde base 10 logaritdm of E (approx. 0.434) |
PI | It will return constant value of PI (approx. 3.14) |
SQRT1_2 | It will return tde square root of 1/2 (approx. 0.707) |
SQRT2 | It will returnstde square root of 2 (approx. 1.414) |
Math Object Methods
Mathod | Description |
---|---|
abs(valuex) | It will return the absolute value of valuex |
acos(valuex) | It will return the arccosine of valuex, in radians |
asin(valuex) | It will return the arcsine of valuex, in radians |
atan(valuex) | It will return the arctangent of valuex as a numeric value between -PI/2 and PI/2 radians |
atan2(valuey, valuex) | It will return the arctangent of the quotient of its arguments |
ceil(valuex) | It will return the value of valuex rounded up to its nearest integer |
cos(valuex) | It will return the cosine of valuex (valuex is in radians) |
exp(valuex) | It will return the value of Ex |
floor(valuex) | It will return the value of valuex rounded down to its nearest integer |
log(valuex) | It will return the natural logarithm (base E) of valuex |
max(valuex,value y, valuez, …, valuen) | It will return the number with the highest value |
min(valuex,valuey, valuez, …, valuen) | It will return the number with the lowest value |
pow(valuex, valuey) | It will return the value of valuex to the power of valuey |
random() | It will return a random number between 0 and 1 |
round(valuex) | It will return the value of valuex rounded to its nearest integer |
sin(valuex) | It will return the sine of valuex (valuex is in radians) |
sqrt(valuex) | It will return the square root of valuex |
tan(valuex) | It will return the tangent of an angle |