JS: Power and Roots

By Xah Lee. Date: . Last updated: .

Power and Roots

Math.pow(x, y)
  • x raised to the power of y.
  • Same as x ** y.
console.log(Math.pow(2, 3));
// 8

console.log(2 ** 3);
// 8
Math.sqrt(x)

Square root.

console.log(Math.sqrt(4));
// 2

console.log(Math.sqrt(9));
// 3
Math.cbrt(x)

(new in ECMAScript 2015)

Cube root.

console.log(Math.cbrt(8));
// 2

console.log(Math.cbrt(27));
// 3

To compute nth root, use Math.pow(x, 1/n).

JavaScript. math functions.