PI is a fundamental mathematical constant. The symbol for PI is denoted by the lowercase Greek letter π. This designation is derived from the term for the periphery.
Pi can be expressed as the ratio of a circle's circumference (C) to its diameter (d). This signifies that regardless of the circle's dimensions, the value of Pi will consistently remain the same.
In JavaScript, the Math.PI property provides the value of PI. This property represents the ratio of a circle's circumference to its diameter, which is roughly 3.14159. It is important to note that Math.PI is a property of the Math object and not a method; thus, attempting to utilize it as a function will result in an error.
The constant PI is commonly utilized in mathematical equations. By employing the Math.PI property, one can effortlessly determine the area of a circle and execute various other computations that necessitate the use of the PI value.
Syntax
Math.PI
Let’s examine a few examples that demonstrate the application of the Math.PI property.
Example1
In this illustration, we are displaying the value of the Math.PI property. Please click the provided button to retrieve the value of PI.
<!DOCTYPE html>
<html>
<head>
<title>
Math.PI
</title>
</head>
<body>
<p>
Welcome to the logic-practice.com
</p>
<p> Click the following button to get the value of PI. </p>
<p id = "para"></p>
<button onclick = "get()">
Click me
</button>
<script>
function get()
{
document.getElementById("para").innerHTML = "The value of PI is: " + Math.PI;
}
</script>
</body>
</html>
Output
Upon running the code provided above and subsequently clicking the specified button, the resulting output will be -
Example2
In this illustration, we will determine the area of a circle. The equation utilized to compute the area is ( π r r ), which necessitates the value of PI. Therefore, we will employ the Math.PI property to obtain the value of PI.
Here, the radius of the circle is 8 .
<!DOCTYPE html>
<html>
<head>
<title>
Math.PI
</title>
</head>
<body>
<p>
Welcome to the logic-practice.com
</p>
<p id = "para"></p>
<script>
var r = area(8);
document.getElementById('para').innerHTML = 'The area of circle with radius 8 is: ' + r ;
function area(radius)
{
return Math.PI * radius * radius;
}
</script>
</body>
</html>
Output