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,

  1. We create a variable radius and set it to 5.
  2. We calculate the circumference of the circle using the formula 2 * Math.PI * radius.
  3. The result is stored in the variable circumference.
  4. We log circumference to the console using the console.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,

  1. We create a variable radius and set it to 5.
  2. We calculate the area of the circle using the formula Math.PI * Math.pow(radius, 2).
  3. The result is stored in the variable area.
  4. We log area to the console using the console.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,

  1. We create variables radius and height and set them to 5 and 10 respectively.
  2. We calculate the volume of the cylinder using the formula Math.PI * Math.pow(radius, 2) * height.
  3. The result is stored in the variable volume.
  4. We log volume to the console using the console.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.