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; // 1.4142135623730951
Examples
Basic Usage
console.log(Math.SQRT2);
// 1.4142135623730951
Calculating Diagonal Length
The Pythagorean theorem uses the square root of 2 when calculating diagonals of a square:
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
console.log(Math.sqrt(2) === Math.SQRT2);
// true
Scaling by √2
The golden ratio and √2 are related in design and photography:
// 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:
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:
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
// 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:
// One approximation method
function approximatePi() {
return Math.sqrt(2 * (2 + Math.sqrt2));
}
console.log(approximatePi());
// 3.1449074048195
Common Patterns
Avoiding Repeated Calculations
// 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
// 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:
- Precision — The constant is defined to full floating-point precision
- Readability — Code clearly expresses the mathematical concept
- Standardization — Matches mathematical notation in formulas
- Performance — The value is pre-computed by the JavaScript engine
- Intent — Using
Math.SQRT2signals 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