jsguides

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). The examples that follow walk through the most common geometric operations — circumference, area, and volume — then show radians conversion, canvas drawing, and circular animation. Each case uses Math.PI to keep the formulas obvious and the code free of magic numbers.

Examples

Basic usage

console.log(Math.PI);
// 3.141592653589793

Calculating circle circumference

The circumference of a circle is 2π times the radius. The function below accepts any radius and returns the full distance around the circle. Using Math.PI instead of a hard-coded 3.14159 keeps the formula readable and matches the notation you would see in a textbook or a geometry reference.

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

Area is πr² — the constant multiplied by the radius squared. Writing the formula as Math.PI * radius * radius is direct, but you could also call Math.pow(radius, 2) if you prefer. The three calls below verify the function against known values: radius 5 (≈78.54), radius 7 (≈153.94), and the zero-radius edge case.

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

Volume moves from two dimensions to three with the formula (4/3)πr³. The factor 4/3 can be written as (4/3) and multiplied against Math.PI and the cubed radius. Notice that Math.PI stays the same across all three geometric examples; the constant anchors the formula while the radius and the shape-specific multiplier change.

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

JavaScript trigonometric functions expect angles in radians, but most people think in degrees. The conversion factor is π/180. Multiplying any degree value by (Math.PI / 180) gives you the radian equivalent. The examples below cover a right angle (90°), a half rotation (180°), and a full rotation (360°).

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

The reverse conversion multiplies radians by 180/π. When you call radiansToDegrees(Math.PI), you get exactly 180 — a useful sanity check that confirms the math is correct. The function works cleanly for any radian value, including zero, which returns zero degrees as expected.

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. The snippet below spells out three common angle constants so you can refer to them by name rather than recomputing them inline. Using named variables like fullRotation makes the intent clearer than a raw multiplication every time.

// 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

Animating a point around a circle starts with converting an angle from degrees to radians, then applying cosine and sine to get the x and y offsets. The function below wraps that logic so you pass a center, a radius, and an angle in degrees. Multiply the radius by cos(radians) for the x offset and by sin(radians) for the y offset. Both offsets are relative to the center, so you add them to get the final coordinates.

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:

  1. Precision — The constant is defined to full floating-point precision
  2. Readability — Code is clearer when you use Math.PI instead of magic numbers
  3. Consistency — Calculations are guaranteed to use the same value everywhere
  4. Performance — The value is computed once by the JavaScript engine

When Math.PI is useful

Math.PI is useful anywhere circles, arcs, angles, or wave-like motion show up. That includes geometry, animation, canvas drawing, simulation code, and any calculation that needs a stable conversion between degrees and radians. Because the constant is built in, it avoids repeated magic numbers and keeps formulas easy to read.

It is also a better teaching tool than a hand-typed approximation because the code shows the exact intent. When someone sees 2 * Math.PI, they can tell the calculation is about a full rotation or a circle rather than a guessed decimal. That clarity pays off in both small snippets and larger numeric helpers.

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