Tag Archives: PI

math.pi() in JavaScript

 
We can use math.pi to get the constant value of PI. The Math.PI property represents the ratio of the circumference of a circle to its diameter and the value of the PI is approximately 3.14159.

Because PI is a static property of Math, you always use it as Math.PI, rather than as a property of a Math object you created.

You can check the value of the Pi in JavaScript itself like as below example.

<html>
  <head> 
    <title> Value of PI </title>
  </head>
   <body>
     <script language="JavaScript">
           var result = Math.PI;
          document.write( "The Approximate value of PI is: " + result );
      </script>  
   </body>
</html>

Output will be :
The Approximate value of PI is: 3.141592653589793

The following example is the simple implementation of the formula : A = PI * R* R, where R is the radius of the circle and A is the area of the circle.

<html>
<head>
<title>Find the area and circumference of a circle</title>
</head>
<body>
  <script language="JavaScript">
    <!–
    function CalculateArea(){
        var r =document.form1.txtRadius.value;
        document.write("<P>The area of the circle is " + (r * r * Math.PI) + "</p>");
    }
    –>
    </script>
    <form name=form1>
        Enter the radius of circle:
       <input type="text" name="txtRadius" size=10>
       <br>
       <input type="button" value="Calculate" onClick='CalculateArea()'>  
    </form>
</script>
</body>
</html>

Thanks