Home >>Javascript Tutorial >JavaScript Math Object
The math object in JavaScript bestow various methods and constants to execute mathematical operation. This object doesn’t have any constructors as of date object.
Following is the list of math methods in JavaScript along with a brief description:
Sr No | Methods | Description |
---|---|---|
1 | abs() | This returns the absolute value of the provided number. |
2 | acos() | This returns the arccosine of the provided number in radians. |
3 | asin() | This returns the arcsine of the provided number in radians. |
4 | atan() | This object returns the arc-tangent of the provided number in radians. |
5 | cbrt() | This object returns the cube root of the provided number. |
6 | ceil() | This object returns the smallest integer value, greater than or equal to the provided number. |
7 | cos() | This object returns the cosine of the provided number. |
8 | cosh() | This object returns the hyperbolic cosine of the provided number. |
9 | exp() | This object returns the exponential form of the provided number. |
10 | floor() | This object returns the largest integer value, lower than or equal to the provided number. |
11 | hypot() | This returns the value of the square root of the sum of the squares of a given integer or a number. |
12 | log() | This object returns the natural logarithm of a number or integer. |
13 | max() | This object returns the maximum value of the provided numbers. |
14 | min() | This object returns the minimum value of the provided numbers. |
15 | pow() | This object returns the value of base to the power of exponent. |
16 | random() | This object returns the random number between 0 (inclusive) and 1 (exclusive). |
17 | round() | This object returns the closest integer value of the provided number. |
18 | sign() | This object returns the sign of the provided number |
19 | sin() | This object returns the sine of the provided number. |
20 | sinh() | This object returns the hyperbolic sine of the provided number. |
21 | sqrt() | This object returns the square root of the provided number. |
22 | tan() | This object returns the tangent of the provided number. |
23 | tanh() | This object returns the hyperbolic tangent of the provided number. |
24 | trunc() | This object returns an integer part of the provided number. |
The Math.sqrt (n) in JavaScript in returns the square root of the provided number. Here is an example that best describes the square root method in JavaScript.
Example
Square Root of 15 is: <span id="p1"></span> <script> document.getElementById('p1').innerHTML=Math.sqrt(15); </script>
The Math.random () method in JavaScript returns a random number between 0-1. Here is an example to understand it from a simple point of view.
Example
Random Number is: <span id="p2"></span> <script> document.getElementById('p2').innerHTML=Math.random(); </script>
The Math.pow (m,n) method in JavaScript returns the m to the power of n i.e. mn`, basically it returns the value of the sum of the power applied to a number. Here is an example that will best describe this method:
Example
2 to the power of 3 is: <span id="p3"></span> <script> document.getElementById('p3').innerHTML=Math.pow(2,3); </script>
The Math.floor (n) method in JavaScript returns the lowest integer for a provided number like 2 for 2.7, 4 for 4.6 and so on. Here is an example for this method to let you understand:
Example
Floor of 8.6 is: <span id="p4"></span> <script> document.getElementById('p4').innerHTML=Math.floor(8.6); </script>
The Math.ceil (n) method in JavaScript returns the largest integer for a provided value like 2 for 1.4, 3 for 2.5 and so on. Let's understand this with an example:
Example
Ceil of 2.6 is: <span id="p5"></span> <script> document.getElementById('p5').innerHTML=Math.ceil(2.6); </script>
The Math.round (n) method in JavaScript returns the round-off of an integer that is nearest to the provided number. Just like simple math, if the fractional part is less than 0.5, it goes to the lower value 0 otherwise to upper value 1 like, 2 for 1.7, 4 for 4.4 and so on.
Example
Round of 2.3 is: <span id="p6"></span><br> Round of 2.7 is: <span id="p7"></span> <script> document.getElementById('p6').innerHTML=Math.round(2.3); document.getElementById('p7').innerHTML=Math.round(2.7); </script>
The Math.abs (n) method in JavaScript returns the utter (absolute) value of the provided number like 2 for -2, 5.5 for -5.5 and so on.
Example
Absolute value of -6 is: <span id="p8"></span> <script> document.getElementById('p8').innerHTML=Math.abs(-6); </script>
Example
<script> document.writeln(Math.acos(-1)+"<br>"); document.writeln(Math.acos(0)+"<br>"); document.writeln(Math.acos(0.5)+"<br>"); document.writeln(Math.acos(1)); </script>
Example
<script> document.writeln(Math.asin(-0.5)+"<br>"); document.writeln(Math.asin(0.2)+"<br>"); document.writeln(Math.asin(0.3)+"<br>"); document.writeln(Math.asin(0.1)); </script>
Example
<script> document.writeln(Math.atan(Infinity)+"<br>"); document.writeln(Math.atan(-Infinity)); </script>
Example
<script> document.writeln(Math.cbrt(75)+"<br>"); document.writeln(Math.cbrt(-94)); </script>
Example
<script> document.writeln(Math.cos(0)+"<br>"); document.writeln(Math.cos(Math.PI)); </script>
Example
<script> document.writeln(Math.cosh(-0.5)+"<br>"); document.writeln(Math.cosh(0.6)+"<br>"); document.writeln(Math.cosh(0.2)+"<br>"); document.writeln(Math.cosh(2.1)); </script>
Example
<script> document.writeln(Math.exp(2)+"<br>"); document.writeln(Math.exp(3)+"<br>"); document.writeln(Math.exp(-4)); </script>
Example
<script> document.writeln(Math.hypot(2,4)+"<br>"); document.writeln(Math.hypot(10,18)); </script>
Example
<script> document.writeln(Math.log(12)+"<br>"); document.writeln(Math.log(50)+"<br>"); document.writeln(Math.log(15)); </script>
Example
<script> document.writeln(Math.max(20,35,19,85)+"<br>"); document.writeln(Math.max(-50,-33,-22,-90)); </script>
Example
<script> document.writeln(Math.min(20,35,19,85)+"<br>"); document.writeln(Math.min(-50,-33,-22,-90)); </script>
Example
<script> document.writeln(Math.sign(52)+"<br>"); document.writeln(Math.sign(-82)+"<br>"); document.writeln(Math.sign(0)); </script>
Example
<script> document.writeln(Math.sin(5)+"<br>"); document.writeln(Math.sin(7)); </script>
Example
<script> document.writeln(Math.sinh(-10)+"<br>"); document.writeln(Math.sinh(8)+"<br>"); document.writeln(Math.sinh(9.5)+"<br>"); document.writeln(Math.sinh(28)); </script>
Example
<script> document.writeln(Math.tan(10)+" "); document.writeln(Math.tan(25)); </script>
Example
<script> document.writeln(Math.tanh(-8)+" "); document.writeln(Math.tanh(9)+" "); document.writeln(Math.tanh(1.5)+" "); document.writeln(Math.tanh(5)); </script>
Example
<script> document.writeln(Math.trunc(52.94)+ "<br>"); document.writeln(Math.trunc(0.24)); </script>