JavaScript Tutorial – Math

The Math object allows you to perform mathematical tasks on numbers.

Math.min()

Math.min() returns the lowest value in a list

How to find the smallest number in a list

<script type="text/javascript">
   Math.min(1, 2, 3, 2, -1);  //returns -1
</script>
Try it yourself »

Math.max()

Math.max() returns the biggest value in a list.

How to find the biggest number in a list

<script type="text/javascript">
   Math.min(1, 2, 3, 2, -1);  //returns 3
</script>
Try it yourself »

Math.random()

Math.random() returns a random number between 0 and 1, including 1 but excluding 0.

How to get a random number with Math.random()

<script type="text/javascript">
   Math.random();
</script>
Try it yourself »

Math.round()

Math.round() rounds number to the nearest integer.

How to round number to nearest integer

<script type="text/javascript"> 
   Math.round(1.3);  // returns 1
   Math.round(1.5);  // returns 2
</script>
Try it yourself »

Math.ceil()

Math.ceil() rounds a number up to the nearest integer

How to round a number up to nearest integer

<script type="text/javascript">
   Math.ceil(1.1);  //returns 2
</script>
Try it yourself »

Math.floor()

Math.floor() rounds a number down to the nearest integer

How to round a number down to nearest integer

<script type="text/javascript">
   Math.floor(1.9);  //returns 1
</script>
Try it yourself »