Math.sqrt and Math.cbrt
You'll learn how to calculate square roots and cube roots in JavaScript for geometric calculations, distance formulas, and algebraic operations.
What Are Square and Cube Roots?
A square root asks: "What number multiplied by itself equals this value?" For example, the square root of 16 is 4 because 4 × 4 = 16.
A cube root asks: "What number multiplied by itself three times equals this value?" The cube root of 27 is 3 because 3 × 3 × 3 = 27.
JavaScript provides Math.sqrt() for square roots and Math.cbrt() for cube roots.
Math.sqrt() — Square Roots
Math.sqrt(16); // 4
Math.sqrt(25); // 5
Math.sqrt(2); // 1.4142135623730951
Math.sqrt(0); // 0
Math.sqrt(-4); // NaN (no real square root of negative numbers)
Common use case: Calculating distance between two points using the Pythagorean theorem:
const dx = 3;
const dy = 4;
const distance = Math.sqrt(dx * dx + dy * dy); // 5
Math.cbrt() — Cube Roots
Math.cbrt(27); // 3
Math.cbrt(8); // 2
Math.cbrt(-8); // -2 (works with negatives!)
Math.cbrt(0); // 0
Unlike Math.sqrt(), cube roots handle negative numbers because a negative times itself three times stays negative.
Practical Applications
These methods are essential for geometric calculations, 3D graphics, physics simulations, and solving algebraic equations. For example, finding the side length of a square given its area, or calculating volumes.
const area = 64;
const sideLength = Math.sqrt(area); // 8
const volume = 1000;
const edgeLength = Math.cbrt(volume); // 10
Key Takeaway: Use Math.sqrt() to calculate square roots (returns NaN for negatives) and Math.cbrt() for cube roots (handles negatives correctly) — essential tools for geometric and algebraic calculations.