top of page

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

The Math object includes several mathematical methods.

One common use of the Math object is to create a random number:

 

<!DOCTYPE html>
<html>
<body>

<p>Math.random() returns a random number between 0 and 1.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
function myFunction() {
    document.getElementById("demo").innerHTML = Math.random();
}
</script>

</body>
</html>

 

Math.min() and Math.max()

Math.min() and Math.max() can be used to find the lowest or highest value in a list of argument

Math.min(0, 150, 30, 20, -8, -200);      // returns -200

 

Math.random()

Math.random() returns a random number between 0 (inclusive),  and 1 (exclusive):

Math.random();              // returns a random number

 

Math.round()

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

Math.round(4.7);            // returns 5
Math.round(4.4);            // returns 4

 

Math.ceil()

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

Math.ceil(4.4);             // returns 5

 

Math.floor()

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

Math.floor(4.7);            // returns 4

 

Javascript Math Object

Perform Mathematical Tasks in Your Webpage

bottom of page