JavaScript Math Object


The math object provides you properties and methods for mathematical constants and functions.

Syntax

var pi_value = Math.PI;
var sine_value = Math.sin(45);

The Math Object

The Math object allows you to perform mathematical tasks.

The Math object includes several mathematical methods.


Math Object Method

MethodDescription
abs(x)Returns the absolute value of x
floor(x)Returns x, rounded downwards to the nearest integer
max(x,y,z,...,n)Returns the number with the highest value
min(x,y,z,...,n)Returns the number with the lowest value
pow(x,y)Returns the value of x to the power of y
round(x)Rounds x to the nearest integer
ceil(x)Returns x, rounded upwards to the nearest integer
random()Returns a random number between 0 and 1
sqrt(x)Returns the square root of x

Math.abs()

Returns the absolute value of a number.

Example

<script type="text/javascript">
 function abs_val()
 {
  var a=absolute.num.value;
  var c;
  c = Math.abs(a);
  document.getElementById('n1').value= c;
 }
</script>
<form name="absolute">
<input type="text" name="num"  placeholder="Enter the Value"/>
<input type="text" id="n1"/>
<input type="button" onClick="abs_val()" value="Check"/>
</form>
Output
 = 

Math.floor()

Returns the largest integer less than or equal to a number.

Example

<script type="text/javascript">
 function floor_val()
 {
  var a=floor_form.num.value;
  var c;
  c = Math.floor(a);
  document.getElementById('n1').value= c;
 }
</script>
<form name="floor_form">
<input type="text" name="num"  placeholder="Enter the Value"/>
<input type="text" id="n1"/>
<input type="button" onClick="floor_val()" value="Check"/>
</form>
Output
 = 

Math.max()

Returns the largest of zero or more numbers.

Example

<script type="text/javascript">
 function max_val()
 {
  var a=max_form.num1.value;
  var b=max_form.num2.value;
  var d;
  d = Math.max(a,b);
  document.getElementById('n1').value= d;
 }
</script>
<form name="max_form">
<input type="text" name="num1"  placeholder="Enter the 1st Value"/>
<input type="text" name="num2"  placeholder="Enter the 2nd Value"/>
<input type="text" id="n1"/>
<input type="button" onClick="max_val()" value="Check"/>
</form>
Output
  = 

Math.min()

Returns the smallest of zero or more numbers.

Example

<script type="text/javascript">
 function min_val()
 {
  var a=min_form.num1.value;
  var b=min_form.num2.value;
  var d;
  d = Math.max(a,b);
  document.getElementById('n1').value= d;
 }
</script>
<form name="min_form">
<input type="text" name="num1"  placeholder="Enter the 1st Value"/>
<input type="text" name="num2"  placeholder="Enter the 2nd Value"/>
<input type="text" id="n1"/>
<input type="button" onClick="min_val()" value="Check"/>
</form>
Output
  = 

Math.pow()

Returns base to the exponent power, that is, base exponent.

Example

<script type="text/javascript">
 function pow_val()
 {
  var a=power_form.num1.value;
  var b=power_form.num2.value;
  var d;
  d = Math.pow(a,b);
  document.getElementById('n1').value= d;
 }
</script>
<form name="power_form">
<input type="text" name="num1"  placeholder="Enter the 1st Value"/>
<input type="text" name="num2"  placeholder="Enter the 2nd Value"/>
<input type="text" id="n1"/>
<input type="button" onClick="pow_val()" value="Check"/>
</form>
Output
  = 

Math.round()

Returns a pseudo-random number between 0 and 1.

Example

<script type="text/javascript">
 function round_val()
 {
  var a=form.num.value;
  var d;
  d = Math.round(a);
  document.getElementById('n').value= d;
 }
</script>
<form name="form">
<input type="text" name="num"  placeholder="Enter the Value"/>
<input type="text" id="n"/>
<input type="button" onClick="round_val()" value="Check"/>
</form>
Output
 = 

Math.ceil()

Returns the smallest integer greater than or equal to a number.

Example

<script type="text/javascript">
 function ceil_val()
 {
  var a=form.num.value;
  var d;
  d = Math.ceil(a);
  document.getElementById('n').value= d;
 }
</script>
<form name="form">
<input type="text" name="num"  placeholder="Enter the Value"/>
<input type="text" id="n"/>
<input type="button" onClick="ceil_val()" value="Check"/>
</form>
Output
 = 

Math.random()

Returns a pseudo-random number between 0 and 1.

Example

<script type="text/javascript">
 function random_val()
 {
  var a=form.num.value;
  var d;
  d = Math.random(a);
  document.getElementById('n').value= d;
 }
</script>
<form name="form">
<input type="text" name="num"  placeholder="Enter the Value"/>
<input type="text" id="n"/>
<input type="button" onClick="random_val()" value="Check"/>
</form>
Output
 = 

Math.sqrt()

Returns a pseudo-random number between 0 and 1.

Example

<script type="text/javascript">
 function sqrt_val()
 {
  var a=form.num.value;
  var d;
  d = Math.sqrt(a);
  document.getElementById('n').value= d;
 }
</script>
<form name="form">
<input type="text" name="num"  placeholder="Enter the Value"/>
<input type="text" id="n"/>
<input type="button" onClick="sqrt_val()" value="Check"/>
</form>
Output
 = 

More Math Object Method

MethodDescription
acos(x)Returns the arccosine of x, in radians
asin(x)Returns the arcsine of x, in radians
atan(x)Returns the arctangent of x as a numeric value between -PI/2 and PI/2 radians
atan2(y,x)Returns the arctangent of the quotient of its arguments
cos(x)Returns the cosine of x (x is in radians)
exp(x)Returns the value of Ex
log(x)Returns the natural logarithm (base E) of x
sin(x)Returns the sine of x (x is in radians)
tan(x)Returns the tangent of an angle

Share this article on