JavaScript Math
Math is build-in object that has properties and methods for mathematical constants and functions.
It allows you to perform mathematical tasks on numbers.
Syntax
- varpi_val = Math.PI;
- varsin_val = Math.sin(30);
Examples
Math.pow()
Math.pow(x,y) returns the value of x to the power of y:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.pow()</h2>
<p>Math.pow(x,y) returns the value of x to the power of y:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.pow(6,2);
</script>
</body>
</html>
Try Now
Output
Math.pow(x,y) returns the value of x to the power of y:
36
Math.sqrt()
Math.sqrt(x) returns the square root of x
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.sqrt()</h2>
<p>Math.sqrt(x) returns the square root of x:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.sqrt(81);
</script>
</body>
</html>
Try Now
Output
Math.sqrt(x) returns the square root of x:
9
Math.ceil()
Math.ceil(x) returns the value of x rounded up to its nearest integer:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.ceil()</h2>
<p>Math.ceil() rounds a number <strong>up</strong> to its nearest integer:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.ceil(6.8);
</script>
</body>
</html>
Try Now
Output
Math.ceil() rounds a number up to its nearest integer:
7