The JavaScript Math object allows you to perform mathematical tasks on numbers.
Math.PI
Return ratio of a circle's circumference to its diameter
Example(1)
document.getElementById("demo1").innerHTML = Math.PI;//return ratio of a circle's circumference to its diameter
Math.round()
Math.round(x)
returns the value of x rounded to its nearest integer:
document.getElementById("demo2").innerHTML = Math.round(3.4);//returns the value of x rounded to its nearest integer
Math.pow()
Math.pow(x, y)
returns the value of x to the power of y:
document.getElementById("demo3").innerHTML = Math.pow(4,2);//returns the value of x to the power of y
Math.sqrt()
Math.sqrt(x)
returns the square root of x:
Example(4)
document.getElementById("demo4").innerHTML = Math.sqrt(36);//returns the square root of x
Math.abs()
Math.abs(x)
returns the absolute (positive) value of x:
document.getElementById("demo5").innerHTML = Math.abs(-2.2);//returns the absolute (positive) value of x
Math.ceil()
Math.ceil(x)
returns the value of x rounded up to its nearest integer:
document.getElementById("demo6").innerHTML = Math.ceil(2.3);//rounds a number up to its nearest integer
Math.floor()
Math.floor(x)
returns the value of x rounded down to its nearest integer:
document.getElementById("demo7").innerHTML = Math.floor(6.7);//returns the value of x rounded down to its nearest integer
Math.sin()
Math.sin(x)
returns the sine (a value between -1 and 1) of the angle x (given in radians).
If you want to use degrees instead of radians, you have to convert degrees to radians:
Angle in radians = Angle in degrees x PI / 180.
Example(8)
document.getElementById("demo8").innerHTML =
"The sine value of 90 degrees is " + Math.sin(90 * Math.PI / 180);//returns the sin of x
Math.cos()
Math.cos(x)
returns the cosine (a value between -1 and 1) of the angle x (given in radians).
If you want to use degrees instead of radians, you have to convert degrees to radians:
Angle in radians = Angle in degrees x PI / 180.
Example(9)
document.getElementById("demo9").innerHTML =
"The cosine value of 0 degrees is " + Math.cos(0 * Math.PI / 180);//returns the cos of x
Math.min() and Math.max()
Math.min()
and Math.max()
can be used to find the lowest or highest value in a list of arguments:
Example(10)
document.getElementById("demo10").innerHTML =
Math.min(0, 150, 30, 20, -8, -200);//returns the lowest value in a list of arguments.
document.getElementById("demo11").innerHTML =
Math.max(0, 150, 30, 20, -8, -200);//returns the highest value in a list of arguments.
Math.random()
Math.random()
returns a random number between 0 (inclusive), and 1 (exclusive):
document.getElementById("demo12").innerHTML = Math.random();
Complete Code Of Mathematical Tasks Can Done With Javascript
<!DOCTYPE html>
<html>
<head>
<title>Mathematical Tasks Can Done With Javascript</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<style>
h2{
color:red;
}
</style>
<body>
<div class="container">
<div class="text-center">
<h1>Mathematical Tasks Can Done With Javascript</h1>
</div>
<br>
<div class="well">
<h2>Math.PI</h2>
<h3 id="demo1"></h3>
<h2>Math.round</h2>
<h3 id="demo2"></h3>
<h2>Math.pow</h2>
<h3 id="demo3"></h3>
<h2>Math.sqrt</h2>
<h3 id="demo4"></h3>
<h2>Math.abs</h2>
<h3 id="demo5"></h3>
<h2>Math.ceil</h2>
<h3 id="demo6"></h3>
<h2>Math.floor</h2>
<h3 id="demo7"></h3>
<h2>Math.sin</h2>
<h3 id="demo8"></h3>
<h2>Math.cos</h2>
<h3 id="demo9"></h3>
<h2>Math.min</h2>
<h3 id="demo10"></h3>
<h2>Math.max</h2>
<h3 id="demo11"></h3>
<h2>Math.random</h2>
<h3 id="demo12"></h3>
</div>
<br>
</div>
</body>
</html>
<script>
document.getElementById("demo1").innerHTML = Math.PI;//return ratio of a circle's circumference to its diameter
document.getElementById("demo2").innerHTML = Math.round(3.4);//returns the value of x rounded to its nearest integer
document.getElementById("demo3").innerHTML = Math.pow(4,2);//returns the value of x to the power of y
document.getElementById("demo4").innerHTML = Math.sqrt(36);//returns the square root of x
document.getElementById("demo5").innerHTML = Math.abs(-2.2);//returns the absolute (positive) value of x
document.getElementById("demo6").innerHTML = Math.ceil(2.3);//rounds a number up to its nearest integer
document.getElementById("demo7").innerHTML = Math.floor(6.7);//returns the value of x rounded down to its nearest integer
document.getElementById("demo8").innerHTML =
"The sine value of 90 degrees is " + Math.sin(90 * Math.PI / 180);//returns the sin of x
document.getElementById("demo9").innerHTML =
"The cosine value of 0 degrees is " + Math.cos(0 * Math.PI / 180);//returns the cos of x
document.getElementById("demo10").innerHTML =
Math.min(0, 150, 30, 20, -8, -200);//returns the lowest value in a list of arguments.
document.getElementById("demo11").innerHTML =
Math.max(0, 150, 30, 20, -8, -200);//returns the highest value in a list of arguments.
document.getElementById("demo12").innerHTML = Math.random();
</script>