Wednesday, September 17, 2025
HomeLanguagesJavascriptJavaScript Program to Get the Value of PI

JavaScript Program to Get the Value of PI

In this article, we will explore various ways to get the value of π in JavaScript. The value of π (pi) is a mathematical constant representing the ratio of a circle’s circumference to its diameter. It’s approximately 3.141592653589793 and is used in various mathematical and scientific calculations.

We will explore all the above methods along with their basic implementation with the help of examples.

Approach 1: Using the Math Object

In this approach, we are using the Math object is a built-in global object that provides various mathematical functions and constants, including operations like trigonometry, logarithms, and constants like π (pi).

Syntax:

 const piValue = Math.PI;

Example: here is the basic example of using math.object.

Javascript




const piValue = Math.PI;
console.log("π (pi) Value is :", piValue);


Output

π (pi) Value is : 3.141592653589793

Apporach 2: Using Math.atan()

Javascript Math.atan( ) method is used to return the arctangent of a number in radians. The Math.atan() method returns a numeric value between -pi/2 and pi/2 radians.

Syntax:

Math.atan(value)

Example: In this example we are using the above-explained method.

Javascript




const val = 4 * (Math.atan(1));
console.log("value of π is :", val);


Output

value of π is : 3.141592653589793

Approach 3: Using for..of loop

In this approach, we use a for…of loop with an iterable sequence generated by Array(iterations).keys() to iteratively compute π by alternating fractions, enhancing readability while maintaining the original logic for increased accuracy.

Syntax:

for ( variable of iterableObjectName) {
...
};

Example: In this example we are using above explained approach.

Javascript




function calculatePi(iterations) {
    let pi = 0;
    let sign = 1;
  
    for (let i of Array(iterations).keys()) {
        pi += (4 / (2 * i + 1)) * sign;
        sign *= -1;
    }
  
    return pi;
}
  
// Increase the number of iterations 
// for more accuracy
const pi = calculatePi(1000000);
console.log(pi);


Output

3.1415916535897743
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32299 POSTS0 COMMENTS
Milvus
84 POSTS0 COMMENTS
Nango Kala
6660 POSTS0 COMMENTS
Nicole Veronica
11833 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11895 POSTS0 COMMENTS
Shaida Kate Naidoo
6779 POSTS0 COMMENTS
Ted Musemwa
7050 POSTS0 COMMENTS
Thapelo Manthata
6735 POSTS0 COMMENTS
Umr Jansen
6741 POSTS0 COMMENTS