JavaScript Math PI
Syntax & Examples
Math.PI static-property
The Math.PI property represents the ratio of a circle's circumference to its diameter, which is approximately equal to 3.14159.
Syntax of Math.PI
The syntax of Math.PI static-property is:
Math.PI
This PI static-property of Math ratio of a circle's circumference to its diameter; approximately 3.14159.
✐ Examples
1 Using Math.PI to calculate the circumference of a circle
In JavaScript, we can use the Math.PI
property to calculate the circumference of a circle given its radius.
For example,
- We create a variable
radius
and set it to 5. - We calculate the circumference of the circle using the formula
2 * Math.PI * radius
. - The result is stored in the variable
circumference
. - We log
circumference
to the console using theconsole.log()
method.
JavaScript Program
const radius = 5;
const circumference = 2 * Math.PI * radius;
console.log(circumference);
Output
31.41592653589793
2 Using Math.PI to calculate the area of a circle
In JavaScript, we can use the Math.PI
property to calculate the area of a circle given its radius.
For example,
- We create a variable
radius
and set it to 5. - We calculate the area of the circle using the formula
Math.PI * Math.pow(radius, 2)
. - The result is stored in the variable
area
. - We log
area
to the console using theconsole.log()
method.
JavaScript Program
const radius = 5;
const area = Math.PI * Math.pow(radius, 2);
console.log(area);
Output
78.53981633974483
3 Using Math.PI to calculate the volume of a cylinder
In JavaScript, we can use the Math.PI
property to calculate the volume of a cylinder given its radius and height.
For example,
- We create variables
radius
andheight
and set them to 5 and 10 respectively. - We calculate the volume of the cylinder using the formula
Math.PI * Math.pow(radius, 2) * height
. - The result is stored in the variable
volume
. - We log
volume
to the console using theconsole.log()
method.
JavaScript Program
const radius = 5;
const height = 10;
const volume = Math.PI * Math.pow(radius, 2) * height;
console.log(volume);
Output
785.3981633974483
Summary
In this JavaScript tutorial, we learned about PI static-property of Math: the syntax and few working examples with output and detailed explanation for each example.