Home >>Javascript Tutorial >JavaScript Math Object

JavaScript Math Object

JavaScriprt Math

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.

Math Methods in JavaScript

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.

Examples and Syntax of the above mentioned methods:

Math.sqrt (n) in JavaScript

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>    
Output :Square Root of 15 is: 3.872983346207417

Math.random () in JavaScript

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>   
Output :Random Number is: 0.11003826891061164

Math.pow (m,n) in JavaScript

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>   
Output :2 to the power of 3 is: 8

Math.floor (n) in JavaScript

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>  
Output :Floor of 8.6 is: 8

Math.ceil (n) in JavaScript

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> 
Output :Ceil of 2.6 is: 3

Math.round (n) in JavaScript

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>    
Output :
Round of 2.3 is: 2
Round of 2.7 is: 3

Math.abs (n) in JavaScript

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>   
Output :Absolute value of -6 is: 6

Math.acos() method in JavaScript

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>   
Output :
3.141592653589793
1.5707963267948966
1.0471975511965979
0

Math.asin() method in JavaScript

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>   
Output :
-0.5235987755982989
0.2013579207903308
0.3046926540153975
0.1001674211615598

Math.atan() method in JavaScript

Example

<script>     
document.writeln(Math.atan(Infinity)+"<br>");
document.writeln(Math.atan(-Infinity));
</script>   
Output :
1.5707963267948966
-1.5707963267948966

Math.cbrt() method in JavaScript

Example

<script>     
document.writeln(Math.cbrt(75)+"<br>");
document.writeln(Math.cbrt(-94));
</script>   
Output :
4.2171633265087465
-4.546835943776344

Math.cos() method in JavaScript

Example

<script>     
document.writeln(Math.cos(0)+"<br>");
document.writeln(Math.cos(Math.PI));
</script>   
Output :
1
-1

Math.cosh() method in JavaScript

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>   
Output :
1.1276259652063807
1.1854652182422676
1.020066755619076
4.1443131704103155

Math.exp() method in JavaScript

Example

<script>     
document.writeln(Math.exp(2)+"<br>");
document.writeln(Math.exp(3)+"<br>");
document.writeln(Math.exp(-4));
</script>   
Output :
7.38905609893065
20.085536923187668
0.01831563888873418

Math.hypot() method in JavaScript

Example

<script>     
document.writeln(Math.hypot(2,4)+"<br>");
document.writeln(Math.hypot(10,18));
</script>   
Output :
4.47213595499958
20.591260281974

Math.log() method in JavaScript

Example

<script>     
document.writeln(Math.log(12)+"<br>");
document.writeln(Math.log(50)+"<br>");
document.writeln(Math.log(15));

</script>   
Output :
2.4849066497880004
3.912023005428146
2.70805020110221

Math.max() method in JavaScript

Example

<script>     
document.writeln(Math.max(20,35,19,85)+"<br>"); 
document.writeln(Math.max(-50,-33,-22,-90));
</script>   
Output :
85
-22

Math.min() method in JavaScript

Example

<script>     
document.writeln(Math.min(20,35,19,85)+"<br>"); 
document.writeln(Math.min(-50,-33,-22,-90));
</script>   
Output :
19
-90

Math.sign() method in JavaScript

Example

<script>     
document.writeln(Math.sign(52)+"<br>");
document.writeln(Math.sign(-82)+"<br>");
document.writeln(Math.sign(0));
</script>   
Output :
1
-1
0

Math.sin() method in JavaScript

Example

<script>     
document.writeln(Math.sin(5)+"<br>");
document.writeln(Math.sin(7));
</script>   
Output :
-0.9589242746631385
0.6569865987187891

Math.sinh() method in JavaScript

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>   
Output :
-11013.232874703393
1490.4788257895502
6679.86337740502
723128532145.7375

Math.tan() method in JavaScript

Example

<script>     
document.writeln(Math.tan(10)+"
");
document.writeln(Math.tan(25));
</script>   
Output :
0.6483608274590866
-0.13352640702153587

Math.tanh() method in JavaScript

Example

<script>     
document.writeln(Math.tanh(-8)+"
");
document.writeln(Math.tanh(9)+"
");
document.writeln(Math.tanh(1.5)+"
");
document.writeln(Math.tanh(5));
</script>   
Output :
-0.9999997749296758
0.999999969540041
0.9051482536448664
0.9999092042625951

Math.trunc() method in JavaScript

Example

<script>     
document.writeln(Math.trunc(52.94)+ "<br>");
document.writeln(Math.trunc(0.24));
</script>   
Output :
52
0

No Sidebar ads