jsguides

Math.SQRT2

Math.SQRT2 is a static property of the Math object that represents the square root of 2, approximately 1.4142135623730951. It is a read-only constant; you cannot change its value.

The square root of 2 is famous in mathematics — it was the first known irrational number. It appears frequently in geometry, computer graphics, diagonal calculations, and numerical algorithms.

Syntax

Math.SQRT2

Value

Math.SQRT2 is one of several mathematical constants built into JavaScript’s Math object. Its value — approximately 1.414 — is the square root of 2 stored at full double-precision. You cannot reassign it; attempting to do so silently fails in non-strict mode.

Math.SQRT2; // 1.4142135623730951

Examples

Basic Usage

Accessing Math.SQRT2 is straightforward — it behaves like any other read-only property and produces the same value every time. The constant is available in every JavaScript environment with no imports or polyfills needed. It has been part of the language since ES1, so you can use it without worrying about browser support.

console.log(Math.SQRT2);
// 1.4142135623730951

Calculating diagonal length

A square’s diagonal is the geometric application you’ll reach for most often. The Pythagorean theorem uses the square root of 2 when calculating diagonals of a square. For a square with side length s, the diagonal is always s × √2. This constant saves you from recomputing Math.sqrt(2) in every geometry helper. The relationship comes directly from a² + b² = c² where both legs equal the side length, so c = √(s² + s²) = s√2. The example below confirms this for several side lengths and shows how the diagonal grows linearly as the square gets larger.

function diagonalOfSquare(side) {
  return side * Math.SQRT2;
}

console.log(diagonalOfSquare(1));
// 1.4142135623730951

console.log(diagonalOfSquare(10));
// 14.142135623730951

console.log(diagonalOfSquare(100));
// 141.42135623730951

Verifying square roots

Math.sqrt(2) and Math.SQRT2 produce the same value, but the constant avoids the runtime cost of a function call. For hot loops or tight rendering code where every microsecond counts, the pre-computed constant is the better choice. You can verify the equivalence with a single equality check as shown below.

console.log(Math.sqrt(2) === Math.SQRT2);
// true

Scaling by √2

The golden ratio and √2 are related in design and photography through the A-series paper standard. An A4 sheet’s aspect ratio is 1:√2 — fold it in half and you get A5 with the same proportions. This scaling relationship makes Math.SQRT2 useful for layout calculations that need to preserve aspect ratios across different sizes.

// A4 paper aspect ratio uses √2
function aSeriesWidth(height) {
  return height * Math.SQRT2;
}

console.log(aSeriesWidth(297));  // A4 height in mm
// 420 (approximately, for A3 width)

Normalizing Vectors

In 2D graphics, you need √2 to normalize diagonal vectors. A vector pointing at 45 degrees has equal x and y components, so its magnitude is √(1² + 1²) = √2. Dividing each component by √2 produces a unit vector pointing in the same direction.

function normalize2D(x, y) {
  const magnitude = Math.sqrt(x * x + y * y);
  return {
    x: x / magnitude,
    y: y / magnitude
  };
}

// Diagonal normalization needs √2
console.log(normalize2D(1, 1));
// { x: 0.7071067811865476, y: 0.7071067811865476 }
// This equals 1 / Math.SQRT2

Distance Calculations

Euclidean distance between diagonal points uses √2. The distance from the origin to (1, 1) is exactly √2, which makes Math.SQRT2 a useful check value when testing geometry functions. You can use it as an assertion target to verify that a distance calculator handles diagonal input correctly.

function distance2D(x1, y1, x2, y2) {
  return Math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2);
}

// Distance from (0,0) to (1,1) = √2
console.log(distance2D(0, 0, 1, 1));
// 1.4142135623730951

Trigonometric Relationships

At 45 degrees, both cosine and sine equal √2/2. This comes from the unit circle — the point at angle π/4 is (√2/2, √2/2). The identity is a cornerstone of planar geometry and appears in rotation matrices, signal processing, and any code that decomposes a diagonal direction into its axis-aligned components.

// cos(45°) = sin(45°) = 1/√2 = √2/2
console.log(Math.cos(Math.PI / 4));
// 0.7071067811865476

console.log(Math.SQRT2 / 2);
// 0.7071067811865476

Approximating π

The square root of 2 appears in various π approximations. The example below uses an iterative method that converges toward π, but it also demonstrates how the constant shows up in places beyond simple geometry — the same value serves multiple mathematical purposes across different domains.

// One approximation method
function approximatePi() {
  return Math.sqrt(2 * (2 + Math.sqrt2));
}

console.log(approximatePi());
// 3.1449074048195

Where Math.SQRT2 shows up

Math.SQRT2 appears any time a formula needs the diagonal of a unit square or the scale factor for a 45-degree line. That includes geometry helpers, image sizing, vector math, and design calculations that move between width and height. If your code keeps reusing Math.sqrt(2), the constant makes the intent easier to read and keeps the same precise value in every place.

It is also useful in places that are not obviously geometric. A4 paper dimensions, audio calculations, and some probability formulas all use the same constant because they describe proportional relationships rather than a literal length. A named constant helps the reader understand that the value is mathematical, not arbitrary.

Why use the constant

Hardcoding 1.4142135623730951 works, but it makes the formula harder to scan and easier to mistype. Math.SQRT2 tells the reader exactly what the number means. That matters in code reviews and in future refactors, when someone may want to adjust the surrounding math without re-deriving the constant from memory.

Another practical benefit is consistency. When multiple parts of a codebase use the same factor, a shared constant prevents tiny differences in approximations. That is a small thing, but those small things add up when values are chained together across layout, animation, or measurement code.

Common Patterns

Avoiding repeated calculations

Calling Math.sqrt(2) in a hot loop recomputes the same value every iteration. Storing Math.SQRT2 in a variable once and reusing it avoids that overhead — a small optimization that matters in performance-sensitive rendering or physics code.

// Instead of computing sqrt(2) each time
function calculateHypotenuse(a, b) {
  return Math.sqrt(a * a + b * b);
}

// For unit square diagonal, use the constant
const unitDiagonal = Math.SQRT2;

Scale factor calculations

When you double the area of a square, the side length grows by a factor of √2. This relationship is the inverse of the area formula — newSide = originalSide × √2 gives you the side length that produces twice the original area.

// Doubling the area of a square
function sideForDoubleArea(originalSide) {
  return originalSide * Math.SQRT2;
}

console.log(sideForDoubleArea(5));
// 7.071067811865476

Why use Math.SQRT2?

Using Math.SQRT2 instead of hardcoding 1.4142135623730951 provides several advantages:

  1. Precision — The constant is defined to full floating-point precision
  2. Readability — Code clearly expresses the mathematical concept
  3. Standardization — Matches mathematical notation in formulas
  4. Performance — The value is pre-computed by the JavaScript engine
  5. Intent — Using Math.SQRT2 signals to other developers that √2 is intentionally used

See Also

  • Math.sqrt() — returns the square root of a number
  • Math.PI — the ratio of circle circumference to diameter
  • Math.LN2 — the natural logarithm of 2