Home » JavaScript » JavaScript Math

JavaScript Math Object

The JavaScript Math object is used to perform mathematical operations on numbers. All the methods of Math can be called directly without creating Math object.

Math.PI()

The value of PI is used various times during mathematical operations, but we don't have any particular key in the keyboard to use that value value. The Math.PI property gives the value of PI (π) constant. It is a mathematical constant, which is approximately 3.14159.


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> JavaScript Math Object PI Property </title>
</head>
<body>
<script>
// Printing PI value
document.write(Math.PI + "<br>"); // Prints: 3.141592653589793
// Function to calculate circle area
function calculateCircleArea(radius){
var area = (Math.PI) * radius * radius;
return area;
}
document.write(calculateCircleArea(5) + "<br>"); // Prints: 78.53981633974483
document.write(calculateCircleArea(10) + "<br>"); // Prints: 314.1592653589793
</script>
</body>
</html>

Output

JavaScript Math Object PI Property

Math.round()

The Math.round(x) gives the rounded off value of x to its nearest integer. It will always give rounded off integer values of the specified decimal/fraction values. The values till '.5' will be rounded off to the previous smaller integer and greater than '.5' will be rounded off to the next higher integer. Check out the example below in 'try-it' editor and try finding out results of different values after rounding off.


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> JavaScript Math Object Round Property </title>
</head>
<body>
<h2>JavaScript Math.round() Method</h2>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.round(3.4);
</script>
</body>
</html>

Output

JavaScript Math Object Round Property


Math.pow()

Math.pow(x,y) method returns the value of 'x to the power of y'. Using powers is very common in mathematics and javascript provide this method, so one can easily find out the results. Be careful when specifying numbers in this function, 'x' is the place of the number and 'y' is the place of the given power of the number.


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> JavaScript Math Object Round Property </title>
</head>
<body>
<h2> JavaScript Math.pow() Method </h2>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.pow(2,10);
</script>
</body>
</html>

Output

JavaScript Math Object Round Property


Math.sqrt()

Math.sqrt(n) method returns the square root of the given number(n). If the specified number doesn't have a proper square root then, it will give value in decimals.


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> JavaScript Math Object Square Property </title>
</head>
<body>
<h2> JavaScript Math.sqrt() Method </h2>
<p id="square"></p>
<script>
document.getElementById("square").innerHTML = Math.sqrt(1024);
</script>
</body>
</html>

Output

JavaScript Math Object Square Property


Math.abs()

Math.abs() method is used to calculate the absolute(positive) value of a number. It will always give positive value of the specified number even if it's a negative one.

In this, -5 is returned as 5, -10 as 10, etc.


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> JavaScript Math Object Abs Property </title>
</head>
<body>
<h2> JavaScript Math.abs() Method </h2>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.abs(-10.5);
</script>
</body>
</html>

Output

JavaScript Math Object Abs Property


Math.floor()

The Math.floor() method rounds off a number down to the previous lower integer. Here, even if the decimal exceeds the '.5' value, it will always be rounded off to lower integer. So, 1.8 becomes 1, -2.5 becomes -3.


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> JavaScript Math Object Floor Property </title>
</head>
<body>
<h2> JavaScript Math.floor() Method </h2>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.floor(5.7);
</script>
</body>
</html>

Output

JavaScript Math Object Floor Property


Math.ceil()

The Math.ceil() method rounds a number up to the next highest integer. Here, even if the decimal value is lower than '.5', it will always be rounded off to the next highest integer. So, 1.3 becomes 2, -2.5 becomes -2.


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> JavaScript Math Object Celi Property </title>
</head>
<body>
<h2> JavaScript Math.ceil() Method </h2>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.ceil(10.4);
</script>
</body>
</html>

Output

JavaScript Math Object Celi Property


Math.min()

Math.min() is used to find the lowest value in a list of arguments specified. Look at the example below to see the syntax :-


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> JavaScript Math Object Min Property </title>
</head>
<body>
<h2> JavaScript Math.min() Method </h2>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.min(0, 500, 45, 56, -6, -100)
</script>
</body>
</html>

Output

JavaScript Math Object Min Property


Math.max()

The Math.max() used to find the highest value in a list of arguments specified.


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> JavaScript Math Object Max Property </title>
</head>
<body>
<h2> JavaScript Math.max() Method </h2>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.max(0, 500, 45, 56, -6, -100)
</script>
</body>
</html>

Output

JavaScript Math Object Max Property












Follow Us: