JS: Power and Roots
Power and Roots
Math.pow(x, y)-
x raised to the power of y.
🛑 warning: does not support bigint.
console.log(Math.pow(2, 3)); // 8 // the ** operator supports bigint console.log(3n ** 2n); // 9n // Math.pow() does not support bigint try { Math.pow(3n, 2n); } catch (xerr) { console.log(xerr); } // TypeError: Cannot convert a BigInt value to a number 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).