It allows you to perform
mathematical tasks on numbers .Unlike the other global objects, Math is not a
constructor. All properties and methods of Math are static. You refer to the
constant pi as Math.PI and you call the sine function as Math.sin(x), where x
is the method's argument. Constants are defined with the full precision of real
numbers in JavaScript.The JavaScript math object provides several constants and
methods to perform mathematical operation. Unlike date object, it doesn't have
constructors.
Examples
Methods Name
|
Description
|
Math.abs(x)
|
Returns
the absolute value of a number.
|
Math.acos(x)
|
Returns
the arccosine of a number.
|
Math.acosh(x)
|
Returns
the hyperbolic arccosine of a number.
|
Math.asin(x)
|
Returns
the arcsine of a number.
|
Math.asinh(x)
|
Returns
the hyperbolic arcsine of a number.
|
Math.atan(x)
|
Returns
the arctangent of a number.
|
Math.atanh(x)
|
Returns
the hyperbolic arctangent of a number.
|
Math.cbrt(x)
|
Returns
the cube root of a number.
|
Math.cos(x)
|
Returns
the cosine of a number.
|
Math.atan2(y,
x)
|
Returns
the arctangent of the quotient of its arguments.
|
Math.clz32(x)
|
Returns
the number of leading zeroes of a 32-bit integer.
|
Math.cosh(x)
|
Returns
the hyperbolic cosine of a number.
|
Math.ceil(x)
|
Returns
the smallest integer greater than or equal to a number.
|
Math.exp(x)
|
Returns
Ex, where x is the argument, and E is Euler's constant (2.718…), the base of
the natural logarithm.
|
Math.expm1(x)
|
Returns
subtracting 1 from exp(x).
|
Math.floor(x)
|
Returns
the largest integer less than or equal to a number.
|
Math.fround(x)
|
Returns
the nearest single precision float representation of a number.
|
Math.hypot([x[,
y[, …]]])
|
Returns
the square root of the sum of squares of its arguments.
|
Math.imul(x,
y)
|
Returns
the result of a 32-bit integer multiplication.
|
Math.log(x)
|
Returns
the natural logarithm (loge, also ln) of a number.
|
Math.log1p(x)
|
Returns
the natural logarithm (loge, also ln) of 1 + x for a number x.
|
Math.log10(x)
|
Returns
the base 10 logarithm of a number.
|
Math.log2(x)
|
Returns
the base 2 logarithm of a number.
|
Math.max([x[,
y[, …]]])
|
Returns
the largest of zero or more numbers.
|
Math.min([x[,
y[, …]]])
|
Returns
the smallest of zero or more numbers.
|
Math.pow(x,
y)
|
Returns
base to the exponent power, that is, baseexponent.
|
Math.round(x)
|
Returns
the value of a number rounded to the nearest integer.
|
Math.random()
|
Returns
a pseudo-random number between 0 and 1.
|
Math.sinh(x)
|
Returns
the hyperbolic sine of a number.
|
Math.sign(x)
|
Returns
the sign of the x, indicating whether x is positive, negative or zero.
|
Math.sqrt(x)
|
Returns
the positive square root of a number.
|
Math.sin(x)
|
Returns
the sine of a number.
|
Math.tan(x)
|
Returns
the tangent of a number.
|
Math.toSource()
|
Returns
the string "Math".
|
Math.tanh(x)
|
Returns
the hyperbolic tangent of a number.
|
Math.trunc(x)
|
Returns
the integral part of the number x, removing any fractional digits.
|
Math.PI
Math.PI returns the ratio of a
circle's circumference to its diameter
<!DOCTYPE html>
<html>
<body>
<h2>Math.PI</h2>
<p>Math.PI
returns the ratio of a circle's circumference to its diameter:</p>
<p id="output"></p>
<script>
document.getElementById("output").innerHTML
= Math.PI;
</script>
</body>
</html>
The
Output will be –
|
Math.PI
|
Math.abs(n)
The JavaScript math.abs(n) method
returns the absolute value for the given number
Absolute
value of -4 is: <span id="p8"></span>
<script>
document.getElementById('p8').innerHTML=Math.abs(-4);
</script>
|
Output:
Absolute value of -4 is: 4
Math.random()
The JavaScript math.random()
method returns the random number between 0 to 1.
Random Number is:
<span id="para11"></span>
<script>
document.getElementById('para11').innerHTML=Math.random();
</script>
|
Output:
Random Number is: 0.6929413891917615
Math.pow(m,n)
The JavaScript math.pow(m,n)
method returns the m to the power of n that is mn.
3 to the power of 4 is:
<span id="ppp33"></span>
<script>
document.getElementById('ppp33').innerHTML=Math.pow(3,4);
</script>
|
Output:
3 to the power of 4 is: 81
Math.sqrt(n)
The JavaScript math.sqrt(n)
method returns the square root of the given number.
Square Root of 17 is:
<span id="paragraph1"></span>
<script>
document.getElementById('paragraph1').innerHTML=Math.sqrt(17);
</script>
|
Output:
Square Root of 17 is: 4.123105625617661
Math.floor(n)
The JavaScript math.floor(n)
method returns the lowest integer for the given number. Floor of 4.6 is:
<span id="para444"></span>
<script>
document.getElementById('para444').innerHTML=Math.floor(4.6);
</script>
|
Output:
Floor of 4.6 is: 4
Math.round(n)
The JavaScript math.round(n)
method returns the rounded integer nearest for the given number.
Round
of 4.3 is: <span id="para6"></span><br>
Round
of 4.7 is: <span id="para7"></span>
<script>
document.getElementById('para6').innerHTML=Math.round(4.3);
document.getElementById('para7').innerHTML=Math.round(4.7);
</script>
|
Output:
Round of 4.3 is: 4
Round of 4.7 is: 5
Math.ceil(n)
The JavaScript math.ceil(n)
method returns the largest integer for the given number.
Ceil of 4.6 is:
<span id="para5"></span>
<script>
document.getElementById('para5').innerHTML=Math.ceil(4.6);
</script>
|
Output:
Ceil of 4.6 is: 5