Course contentsShow
← JavaScript
Lesson 142 of 2,7234. Working with NumbersFree lesson

Math.sqrt and Math.cbrt for Roots

Computing square roots and cube roots of numbers using dedicated methods.

Math.sqrt and Math.cbrt for Roots

What you'll learn: How to calculate square roots and cube roots of numbers using JavaScript's built-in Math methods.

The Core Idea

Sometimes you need to reverse exponentiation—instead of asking "what's 4 squared?" you need to ask "what number squared equals 16?" JavaScript provides two dedicated methods for the most common root calculations:

  • Math.sqrt(x) computes the square root (the number that, when squared, equals x)
  • Math.cbrt(x) computes the cube root (the number that, when cubed, equals x)

Math.sqrt for Square Roots

The square root is the inverse of squaring a number. If 5² = 25, then √25 = 5.

Math.sqrt(25);    // 5
Math.sqrt(144);   // 12
Math.sqrt(2);     // 1.414213562373095
Math.sqrt(0);     // 0
Math.sqrt(-1);    // NaN (no real square root of negative numbers)

Math.cbrt for Cube Roots

The cube root is the inverse of cubing a number. If 2³ = 8, then ∛8 = 2.

Math.cbrt(8);     // 2
Math.cbrt(27);    // 3
Math.cbrt(64);    // 4
Math.cbrt(-8);    // -2 (cube roots work with negative numbers!)

Practical Example

// Calculate the side length of a square given its area
let area = 49;
let sideLength = Math.sqrt(area);  // 7

// Calculate the side length of a cube given its volume
let volume = 125;
let cubeSide = Math.cbrt(volume);  // 5

Key Takeaway: Use Math.sqrt(x) to find square roots and Math.cbrt(x) to find cube roots—they're the inverse operations of squaring and cubing numbers, respectively.