Google News
logo
Javascript Math
The JavaScript math object provides several constants and methods to perform mathematical operation. They are following :
Javascript Math.abs() :
<html>
<head>
<title>Javascript Math.abs()</title>
</head>
<body>
 
 
<h4>Absolute value of -9 is : <span id="abs_val"></span>   </h4>
  
<script type="text/javascript">
 
document.getElementById('abs_val').innerHTML=Math.abs(-9);      
 
</script>
</body>
</html>
Output :

Absolute value of -9 is : 9

Javascript Math.ceil() :
<html>
<head>
<title>Javascript Math.ceil()</title>
</head>
<body>
<script type="text/javascript">
 
 var value = Math.ceil(17.95);
 document.write("First Value : " + value ); 
 
 var value = Math.ceil(17.20);
 document.write("<br />Second Value : " + value ); 
 
 var value = Math.ceil(-9.95);
 document.write("<br />Third Value : " + value ); 
 
 var value = Math.ceil(-9.20);
 document.write("<br />Fourth Value : " + value ); 
 
</script>
</body>
</html>
Output :

First Value : 18

Second Value : 18

Third Value : -9

Fourth Value : -9

Javascript Math.floor() :
<html>
<head>
<title>Javascript Math.floor()</title>
</head>
<body>
 
 
<script type="text/javascript">
 
var value = Math.floor(18.9);
document.write("First Value : " + value );
 
var value = Math.floor(26.1);
document.write("<br />Second Value : " + value ); 
 
var value = Math.floor(-5.8);
document.write("<br />Third Value : " + value ); 
 
var value = Math.floor(-2.6);
document.write("<br />Fourth Value : " + value ); 
 
</script>
 
</body>
</html>
Output :

First Value : 18

Second Value : 26

Third Value : -6

Fourth Value : -3

Javascript Math.max() and Math.min() :
<html>
<head>
<title>Javascript Math.max() and Math.min()</title>
</head>
<body>
 
  
<script type="text/javascript">
 
 //Maximum Value
 var value = Math.max(2, 13, -8, 27);
 document.write("First Max Value : " + value ); 
 
 var value = Math.max(-1, -16, -5);
 document.write("<br />Second Max Value : " + value ); 
 
 var value = Math.max(0, -5);
 document.write("<br />Third Max Value : " + value ); 
 
 
 //Minimum value
 var value = Math.min(2, 13, -8, 27);
 document.write("<br /><br />First Min Value : " + value );
 
 var value = Math.min(-1, -16, -5);
 document.write("<br />Second Min Value : " + value ); 
 
 var value = Math.min(0, -5);
 document.write("<br />Third Min Value : " + value ); 
 
 
 
</script>
</body>
</html>
Output :

First Max Value : 27

Second Max Value : -1

Third Max Value : 0


First Min Value : -8

Second Min Value : -16

Third Min Value : -5

Javascript Math.sqrt(n) :
<html>
<head>
<title>Javascript Math.sqrt(n)</title>
</head>
<body>
 
Square Root of 6 is: <span id="squ"></span>    
 
<script type="text/javascript">
 
document.getElementById('squ').innerHTML=Math.sqrt(6);   
 
</script>   
 
</body>
</html>
Output :

Square Root of 6 is: 2.449489742783178

Javascript Math.random() :
<html>
<head>
<title>Javascript Math.random()</title>
</head>
<body>
 
<h4>Random Number is : <span id="ran"></span> </h4>
 
<script type="text/javascript">
 
document.getElementById('ran').innerHTML=Math.random(2);  
 
</script>   
 
</body>
</html>
Output :

Random Number is : 0.16699073430124134

Javascript Math.pow() :
<html>
<head>
<title>Javascript Math.pow()</title>
</head>
<body>
 
<h4>2 power 4 is : <span id="h4"></span>    </h4>
 
<script type="text/javascript">
 
document.getElementById('h4').innerHTML=Math.pow(2,4); 
 
</script>   
 
 
</body>
</html>
Output :

2 power 4 is : 16

Javascript Math.round() :
<html>
<head>
<title>Javascript Math.round()</title>
</head>
<body>
 
 
<h4> Round of 8.2 is: <span id="rd"></span></h4>
<h4> Round of 8.6 is: <span id="rd_2"></span>  </h4>
  
<script type="text/javascript">
 
document.getElementById('rd').innerHTML=Math.round(8.2);   
document.getElementById('rd_2').innerHTML=Math.round(8.6);    
 
</script>
</body>
</html>
Output :

Round of 8.2 is : 8

Round of 8.6 is : 9