Open In App

Find Circumference of a Circle using JavaScript

Last Updated : 13 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The Circumference of the Circle is: 2*pi*r. We can calculate this value in JavaScript using various approaches that are implemented below with examples and output.

These are the following approaches:

Using Arithmetic Operations

In this approach, we are using the formula for the circumference of a circle, which is given by 2Ï€r, where r is the radius. By assigning the radius r=2 and performing the arithmetic operations, we calculate the circumference and display the result with three decimal places using toFixed(3).

Syntax:

let res = 2 * 3.1416 * r;

Example: The below example uses Arithmetic Operations to find the Circumference of a Circle in JavaScript.

Javascript
let r = 2;
let res = 2 * 3.1416 * r;
console.log(`${res.toFixed(3)}`);

Output
12.566

Using Math.PI

In this approach, we use the Math.PI property, representing the mathematical constant pi (approximately 3.141592653589793). By assigning the radius r=2 and using the formula 2×Math.PI×r, the program calculates the circumference of the circle and prints the result with three decimal places using toFixed(3).

Example: The below example uses Math.PI to find the Circumference of a Circle in JavaScript.

Javascript
const r = 2;
const res = 2 * Math.PI * r;
console.log(`${res.toFixed(3)}`);

Output
12.566

Using Math.PI and Math.pow

In this approach, the program uses the Math.PI property for the constant pi and the Math.pow method to calculate the square of the radius. By assigning the radius r=2 and using the formula π×r2 the program calculates the area of the circle and displays the result with three decimal places using toFixed(3).

Example: The below example uses Using Math.PI and Math.pow to find the Circumference of a Circle in JavaScript.

Javascript
const r = 2;
const res = Math.PI * Math.pow(r, 2);
console.log(`${res.toFixed(3)}`);

Output
12.566

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads