Math.SQRT2

Holds the square root of 2, roughly 1.4142135623730951.

Signature
Math.SQRT2
  • Added in JavaScript ES1

Return value

numberThe closest double-precision value to √2, 1.4142135623730951.

Errors

Nothing on read. Assigning to it is silently ignored in sloppy mode and throws a TypeError in strict mode or a module, because the property is non-writable.

Math.SQRT2 is a static data property holding the closest double to √2. MDN describes it as a more performant equivalent to Math.sqrt(2), and the two are the same value — Math.SQRT2 === Math.sqrt(2) is true — so the constant simply saves a call.

When to reach for it

Use it wherever a diagonal at 45° shows up: the diagonal of a square of side s is s * Math.SQRT2, and the ISO A paper series is built entirely on this ratio, so A4's 297 mm long edge is (near enough) its 210 mm short edge scaled by √2. It is also handy for normalising a two-axis input where both axes are at full deflection.

What trips people up

The constant is rounded, so squaring it does not give 2: Math.SQRT2 * Math.SQRT2 is 2.0000000000000004. And 1 / Math.SQRT2 is not bit-identical to Math.SQRT1_2 — they differ by one unit in the last place (0.7071067811865475 versus 0.7071067811865476), which is enough to break a strict equality check. Use Math.SQRT1_2 when you want that reciprocal.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Computing the diagonal length of a square or a 45° offset in a layout
  • Scaling between ISO A paper sizes, where each step is a factor of √2
  • Normalising a two-axis input so diagonal movement is not faster than straight movement
  • Setting the corner radius or bounding box of a shape rotated 45 degrees

Frequently asked questions

Is Math.SQRT2 the same as Math.sqrt(2)?

Yes — both evaluate to 1.4142135623730951 and compare equal with ===. The constant just avoids the function call, which is why MDN calls it the more performant equivalent.

Why does Math.SQRT2 * Math.SQRT2 not equal 2?

Because √2 is irrational and the stored double is a rounded approximation, so squaring it produces 2.0000000000000004. Round the result or compare with a tolerance if you need an exact 2.

Is 1 / Math.SQRT2 the same as Math.SQRT1_2?

No — they differ by one unit in the last place, 0.7071067811865475 versus 0.7071067811865476, so a strict equality check between them fails. Use Math.SQRT1_2 directly when you want that value.

More examples

The A paper series ratio

A4's actual ratio is 1.4142857142857144 because the millimetre dimensions are rounded to whole numbers, while Math.SQRT2 is 1.4142135623730951. Dividing 297 by √2 gives 210.0107140124046 — the unrounded A5 long edge — showing how close the standard sits to the ideal.

Example · javascript
const a4 = { width: 210, height: 297 };
console.log(a4.height / a4.width);
console.log(Math.SQRT2);

// Halving the long edge gives the next size down
console.log(a4.height / Math.SQRT2);
console.log(a4.width * Math.SQRT2);

Normalising diagonal movement

Pressing one direction moves 5 units; pressing two at once still moves 5 total instead of the 7.07 an unnormalised sum would give. Math.hypot(1, 1) returns exactly the same double as Math.SQRT2, which is the length being divided out.

Example · javascript
const speed = 5;
const move = (dx, dy) => {
  const len = Math.hypot(dx, dy);
  return len === 0 ? [0, 0] : [(dx / len) * speed, (dy / len) * speed];
};

console.log(move(1, 0));
console.log(move(1, 1));
console.log(Math.hypot(1, 1) === Math.SQRT2);

Powers of the square root of two

Squaring gives 2.0000000000000004 and the fourth power drifts further to 4.000000000000001. The 0.5 power route lands on the identical double as the constant, so that comparison is true. Rounding is the practical fix when an exact integer is expected.

Example · javascript
console.log(Math.SQRT2 ** 2);
console.log(Math.SQRT2 ** 4);
console.log(2 ** 0.5 === Math.SQRT2);
console.log(Math.round(Math.SQRT2 ** 2));

Discussion

  • Be the first to comment on this lesson.