Math Object
Math.round()

This method round a number to the nearest interger

Example:-
<script language="javascript" type="text/javascript">
var x1=4.2;
var x2=-4.2;
var x3=4.7;
var x4=-4.9;
document.write(Math.round(x1)+"<br>");
document.write(Math.round(x2)+"<br>");
document.write(Math.round(x3)+"<br>");
document.write(Math.round(x4)+"<br>");
</script>

Output:-
4
-4
5
-5


Math.sqrt()

This method find the square root of the number

Example:-
<script language="javascript" type="text/javascript">
var x=4;
document.write(Math.sqrt(x));
</script>

Output:-
2

Math.max()

This method find the greater value.

Example:-
<script language="javascript" type="text/javascript">
var x=4;
var y=8;
var z=2;
document.write(Math.max(x,y,z));
</script>

Output:-
8

Math.min()

This method find the smaller value.

Example:-
<script language="javascript" type="text/javascript">
var x=4;
var y=8;
var z=2;
document.write(Math.min(x,y,z));
</script>

Output:-
2

Math.pow()

This method find the power of the value like(x^y).

Example:-
<script language="javascript" type="text/javascript">
var x=4;
var y=3;
document.write(Math.pow(x,y));
</script>

Output:-
64

Math.random()

This method return a random number between 0 and 1

Example:-
<script language="javascript" type="text/javascript">
document.write(Math.random());
</script>

Output:-
0.5215146250275815

Math.floor()

This method rounds the number to the nearest interger in downwards direction

Example:-
<script language="javascript" type="text/javascript">
var x1=0.78;
var x2=4.2;
document.write(Math.floor(x1)+"<br>");
document.write(Math.floor(x2)+"<br>");
</script>

Output:-
0
4


Previous
Next