
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get the Value of PI in JavaScript
In this tutorial, we will learn how to get the value of PI in JavaScript.
The PI is a mathematical constant that is widely used in different sections of mathematics. It is defined by the ratio of the circumference of a circle to its diameter, which is approximately 3.14159.
It is represented by the symbol ??', which is a Greek letter.
The value of PI is an Irrational number, which means the value of the PI can't be expressed as a fraction, and the digits that came after the decimal is non?terminating and non?repeating.
The value of PI in JavaScript can be found using Math.PI. It helps the mathematical calculations to be performed easily.
Using the Math.PI Property
In JavaScript, The Math.PI property is used to get the value of PI. As it is a property of the built?in Math object, it does not require any parameters to provide the value of PI for quickly accessing the value of PI in calculation Math.PI is very helpful.
Users can follow the below syntax to find the value of PI using Math.PI property.
Syntax
Math.PI
Return Type
The mathematical constant PI (?) which has an approximate value of 3.141592653589793
Example
In the below example, we have used Math.PI property to get the value of PI.
<html> <body> <h4>Get the value of PI using <i> Math.PI </i> property in JavaScript</h4> <div id="root"></div> <script> let root = document.getElementById('root') root.innerHTML = 'The value of PI is: ' + Math.PI </script> </body> </html>
In the above output, users can see that we are accessing the div with the id ?root' by document.getElementById() method and changing its inner text to ?The value of PI is: ' and then using the Math.PI property to get the value of PI.
Application of PI in JavaScript
Calculating Circumference and Area of a Circle
The formula for the circumference of a circle is 2?r. Where the ?r' is the radius of the circle and ??' is the mathematical constant.
Similarly, the formula of the area of a circle is ?r2 (? * r * r). where the ?r' is the radius of the circle, and ??' is the mathematical constant.
In JavaScript, by using Math.PI property, we can easily find out the circumference of a circle and also the area of a circle.
Syntax
let radius = 4 let circumference = 2 * Math.PI * radius let area = Math.PI * radius * radius
In the above code, we are taking a variable radius that stores the radius of the circle. And by using Math.PI, we are calculating the circumference and area of the circle and also storing it in separate variables.
Algorithm
Step 1 ? Declare a variable that stores the radius of the circle.
Step 2 ? Calculate the circumference of the circle by the formula and Math.PI (2*Math.PI*radius) and store the value in a variable.
Step 3 ? Calculate the area of the circle by the formula and Math.PI (Math.PI*radius*radius) and store the value in a variable.
Step 4 ? Console logs the values of circumference and area.
Example
In the below example, we find out the circumference and area of a circle using the it's standard formula and Math.PI property
<html> <body> <h4>Find the value of Circumference and Area of a Circle using <i>Math.PI</i> property in JavaScript</h4> <div id = "root"></div> <script> let radius = 10 let circumference = 2 * Math.PI * radius let area = Math.PI * radius * radius let root = document.getElementById('root') root.innerHTML ='The Circumference of a Circle of Radius ' +radius +' is: ' +(2 * Math.PI * radius) + '<br>' root.innerHTML +='The Area of a Circle of Radius ' +radius +' is: ' +(Math.PI * radius * radius) </script> </body> </html>
In the above output, users can see that we have taken a radius of a circle as ten by using the formula and Math.PI, we can find the circumference and area of a circle. And after calculating these values, we access the div with the id as root using document.getElementById method and change the inner text as per desired output containing the values of circumference and area.
We have learned how to get the value of PI in JavaScript. We used Math.PI property for getting the value of PI is also the recommended way of doing the same. We see how we can use it in mathematical equations and also calculate the circumference and area of a circle. It is a very useful property for mathematical calculations.