Ship internal tools in hours, not weeks. Real auth, users, jobs, audit logs, and cohesive UI included. Early access $249 $499 → [Get it now]

How to get the square root of a number in JavaScript

Calculating square roots is important for geometric calculations, distance formulas, statistical analysis, and data visualization in JavaScript applications. With over 25 years of experience in software development and as the creator of CoreUI, I’ve used square root calculations in geometric utilities, chart scaling, and physics-based animations. For rounding results before display, see how to round a number in JavaScript.

Use Math.sqrt() to calculate the square root of a number.

Math.sqrt(16)    // 4
Math.sqrt(2.25)  // 1.5
Math.sqrt(2)     // 1.4142135623730951
Math.sqrt(0)     // 0
Math.sqrt(-1)    // NaN — negative numbers have no real square root

1. Basic Usage and Edge Cases

Math.sqrt(9)          // 3
Math.sqrt(0.25)       // 0.5
Math.sqrt(1)          // 1
Math.sqrt(0)          // 0
Math.sqrt(-4)         // NaN
Math.sqrt(Infinity)   // Infinity
Math.sqrt(NaN)        // NaN
Math.sqrt(null)       // 0   — null coerces to 0
Math.sqrt(undefined)  // NaN
Math.sqrt('9')        // 3   — numeric strings are coerced
Math.sqrt('hello')    // NaN

Validate inputs with Number.isNaN() when the source is user input or an API.

2. Exponentiation Operator Alternative

The ** 0.5 exponentiation operator (ES2016) is a concise alternative to Math.sqrt() and generalises to any root.

16 ** 0.5      // 4    — square root
27 ** (1/3)    // 3    — cube root
16 ** 0.25     // 2    — fourth root

// Math.sqrt is equivalent for square roots:
Math.sqrt(16) === 16 ** 0.5  // true

Math.sqrt() is typically preferred for square roots due to clarity; use ** (1/n) when you need nth roots.

3. Euclidean Distance with Math.hypot()

For distance calculations involving two or more dimensions, Math.hypot() is more readable and numerically stable than manually squaring, summing, and square-rooting.

// Distance between two points — manual approach
const dist = (x1, y1, x2, y2) => Math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
dist(0, 0, 3, 4)  // 5

// Same with Math.hypot — cleaner and avoids overflow on large values
const distHypot = (x1, y1, x2, y2) => Math.hypot(x2 - x1, y2 - y1)
distHypot(0, 0, 3, 4)  // 5

// 3D distance
Math.hypot(1, 2, 2)  // 3  — √(1² + 2² + 2²) = √9 = 3

Math.hypot() avoids intermediate overflow when components are very large — prefer it over the manual formula in production code.

4. Pythagorean Theorem

function hypotenuse(a, b) {
  return Math.sqrt(a ** 2 + b ** 2)
}

hypotenuse(3, 4)    // 5
hypotenuse(5, 12)   // 13
hypotenuse(1, 1)    // 1.4142135623730951 (√2)

5. Standard Deviation

Square root appears in the standard deviation formula — the final step after computing variance.

function standardDeviation(values) {
  const mean = values.reduce((s, v) => s + v, 0) / values.length
  const variance = values.reduce((s, v) => s + (v - mean) ** 2, 0) / values.length
  return Math.sqrt(variance)
}

standardDeviation([2, 4, 4, 4, 5, 5, 7, 9])  // 2

6. BigInt Limitation

Math.sqrt() does not accept BigInt — it throws a TypeError. Convert to Number first (with precision loss for very large integers).

Math.sqrt(16n)          // TypeError: Cannot convert a BigInt value to a number
Math.sqrt(Number(16n))  // 4  — safe for values within Number.MAX_SAFE_INTEGER

For cryptographic or arbitrary-precision square roots, use a dedicated BigInt math library.

Best Practice Note:

Use Math.hypot() instead of the manual Math.sqrt(dx**2 + dy**2) pattern for distance calculations — it is cleaner and avoids overflow. Always guard against negative inputs when computing square roots; Math.sqrt(-x) silently returns NaN. For absolute value of the difference before squaring, pair Math.abs() and Math.sqrt() together in magnitude calculations.


Speed up your responsive apps and websites with fully-featured, ready-to-use open-source admin panel templates—free to use and built for efficiency.


About the Author