Math.PI
Math.PI is a static property of the Math object that represents the ratio of a circle’s circumference to its diameter — approximately 3.141592653589793. It is a read-only constant; you cannot change its value.
Syntax
Math.PI
Value
Math.PI; // 3.141592653589793
This constant is the same as the mathematical constant π (pi) and is used extensively in geometric calculations involving circles, spheres, and trigonometric functions. Unlike functions like Math.sin() or Math.cos() which require you to know trigonometric concepts, Math.PI is simply a useful shortcut for the ubiquitous π value.
Examples
Basic Usage
console.log(Math.PI);
// 3.141592653589793
Calculating Circle Circumference
The circumference of a circle is calculated using the formula C = 2πr:
function circumference(radius) {
return 2 * Math.PI * radius;
}
console.log(circumference(5));
// 31.41592653589793
console.log(circumference(10));
// 62.83185307179586
console.log(circumference(1));
// 6.283185307179586
Calculating Circle Area
The area of a circle is calculated using the formula A = πr²:
function circleArea(radius) {
return Math.PI * radius * radius;
}
console.log(circleArea(5));
// 78.53981633974483
console.log(circleArea(7));
// 153.93804002589985
console.log(circleArea(0));
// 0
Calculating Sphere Volume
The volume of a sphere uses the formula V = (4/3)πr³:
function sphereVolume(radius) {
return (4/3) * Math.PI * Math.pow(radius, 3);
}
console.log(sphereVolume(1));
// 4.188790204786391
console.log(sphereVolume(2));
// 33.51032163819093
console.log(sphereVolume(10));
// 4188.790204786391
Converting Degrees to Radians
Degrees and radians are two ways to measure angles. To convert degrees to radians:
function degreesToRadians(degrees) {
return degrees * (Math.PI / 180);
}
console.log(degreesToRadians(90));
// 1.5707963267948966
console.log(degreesToRadians(180));
// 3.141592653589793
console.log(degreesToRadians(360));
// 6.283185307179586
Converting Radians to Degrees
To convert radians back to degrees:
function radiansToDegrees(radians) {
return radians * (180 / Math.PI);
}
console.log(radiansToDegrees(Math.PI));
// 180
console.log(radiansToDegrees(Math.PI / 2));
// 90
console.log(radiansToDegrees(0));
// 0
Common Patterns
Drawing a Circle in Canvas
When drawing circles on an HTML5 canvas, Math.PI is essential:
function drawCircle(ctx, x, y, radius) {
ctx.beginPath();
ctx.arc(x, y, radius, 0, 2 * Math.PI);
ctx.stroke();
}
// Draws a circle at (100, 100) with radius 50
drawCircle(ctx, 100, 100, 50);
The ctx.arc() method takes start and end angles in radians, so 2 * Math.PI represents a full circle from 0 to 360 degrees.
Calculating Angles with Trigonometry
While we don’t have dedicated sin() and cos() pages, JavaScript does provide these functions. Math.PI is essential when using them:
// Full rotation (360 degrees)
const fullRotation = 2 * Math.PI;
// Quarter rotation (90 degrees)
const quarterRotation = Math.PI / 2;
// Half rotation (180 degrees)
const halfRotation = Math.PI;
Creating Circular Animations
function getCirclePoint(centerX, centerY, radius, angleInDegrees) {
const radians = angleInDegrees * (Math.PI / 180);
return {
x: centerX + radius * Math.cos(radians),
y: centerY + radius * Math.sin(radians)
};
}
// Get point on circle at 45 degrees
const point = getCirclePoint(100, 100, 50, 45);
console.log(point);
// { x: 135.3553390593274, y: 135.3553390593274 }
Why Use Math.PI?
Using Math.PI instead of manually typing 3.14159... provides several advantages:
- Precision — The constant is defined to full floating-point precision
- Readability — Code is clearer when you use
Math.PIinstead of magic numbers - Consistency — Calculations are guaranteed to use the same value everywhere
- Performance — The value is computed once by the JavaScript engine
See Also
- Math.sqrt() — returns the square root of a number
- Math.pow() — returns the base to the exponent power
- Math.floor() — returns the largest integer less than or equal to a number