Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to 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 are non-terminating and non-repeating.
The value of PI in JavaScript can be found using Math.PI. It helps 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. Math.PI provides quick access to the value of PI for calculations.
Syntax
Math.PI
Return Value
The mathematical constant PI (?) which has an approximate value of 3.141592653589793
Example
In the below example, we use the 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>
The value of PI is: 3.141592653589793
In the above example, we access the div with the id 'root' using document.getElementById() method and change its inner text to display the value of PI using the Math.PI property.
Application of PI in JavaScript
Calculating Circumference and Area of a Circle
The formula for the circumference of a circle is 2?r, where 'r' is the radius of the circle and '?' is the mathematical constant.
Similarly, the formula for the area of a circle is ?r² (? * r * r), where 'r' is the radius of the circle, and '?' is the mathematical constant.
In JavaScript, by using the Math.PI property, we can easily find the circumference and area of a circle.
Syntax
let radius = 4 let circumference = 2 * Math.PI * radius let area = Math.PI * radius * radius
Example
In the below example, we find the circumference and area of a circle using its standard formulas and the 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: ' + circumference.toFixed(2) +
'<br>'
root.innerHTML += 'The Area of a Circle of Radius ' + radius + ' is: ' + area.toFixed(2)
</script>
</body>
</html>
The Circumference of a Circle of Radius 10 is: 62.83 The Area of a Circle of Radius 10 is: 314.16
In the above example, we have taken a radius of 10 and used the formulas with Math.PI to calculate the circumference and area of the circle. We use toFixed(2) to display the results rounded to 2 decimal places for better readability.
Additional Math Operations with PI
Here are more examples showing PI in common mathematical calculations:
<html>
<body>
<h4>Additional PI calculations</h4>
<div id="results"></div>
<script>
let results = document.getElementById('results')
// Converting degrees to radians
let degrees = 180
let radians = degrees * (Math.PI / 180)
// Volume of a sphere: (4/3) * ? * r³
let sphereRadius = 5
let volume = (4/3) * Math.PI * Math.pow(sphereRadius, 3)
results.innerHTML = 'PI value: ' + Math.PI.toFixed(6) + '<br>' +
degrees + ' degrees = ' + radians.toFixed(4) + ' radians<br>' +
'Volume of sphere (r=' + sphereRadius + '): ' + volume.toFixed(2)
</script>
</body>
</html>
PI value: 3.141593 180 degrees = 3.1416 radians Volume of sphere (r=5): 523.60
Conclusion
Math.PI is the standard way to get the value of PI in JavaScript. It's essential for geometric calculations, trigonometry, and various mathematical operations involving circles and angles.
