JavaScript Math.PI



The symbol (π) is derived from the Greek word "periphereia," which means "periphery" or "circumference". Mathematically PI(π) represents the ratio of the circumference of a circle to its diameter. The value of π is approximately 3.14159. In other words, π (3.14159) = circumference of a circle/diameter of a circle.

The JavaScript Math.PI property is representing the ratio of a circumference of a circle to its diameter, approximately 3.14.

Syntax

Following is the syntax of JavaScript Math.PI property −

Math.PI

Return value

This property returns the value of PI.

Example 1

In the following example, we are using the JavaScript Math.PI property to calculate the circumference of a circle −

<html>
<body>
<script>
   const radius = 5;
   const circumference = 2 * Math.PI * radius;
   document.write(circumference);
</script>
</body>
</html>

Output

If we execute the above program, it returns the circumference of the circle "31.41592653589793".

Example 2

Here, we are calculating the area of a circle using the Math.PI property −

<html>
<body>
<script>
   const radius = 3;
   const area = Math.PI * radius * radius;
   document.write(area);
</script>
</body>
</html>

Output

After executing, the area of the circle will be "28.274333882308138".

Example 3

In this example, we are calculating the diameter of a circle using the Math.PI property −

<html>
<body>
<script>
   const circumference = 15;
   const diameter = circumference / Math.PI;
   document.write(diameter);
</script>
</body>
</html>

Output

If we execute the above program, it returns the diameter of the circle "4.7746482927568605".

Example 4

Here, using the Math.PI property we are computing the arc length −

<html>
<body>
<script>
   const radius = 4;
   const angleInRadians = Math.PI / 3; 
   const arcLength = radius * angleInRadians;
   document.write(arcLength);
</script>
</body>
</html>

Output

The arc length will be "4.1887902047863905".

Advertisements