Exponentiation and Roots with Google Apps Script
To calculate the power of a number or its root, you have an operator and functions to do it.
Exponentiation
Calculate the square of a number using the ** operator:
const number = 5;
console.log(number ** 2); // Returns: 25
You can also use the Math.pow() function:
const number = 5;
console.log(Math.pow(number, 2)); // Returns: 25
Calculate the cube of a number:
const number = 5;
console.log(number ** 3); // Returns: 125
console.log(Math.pow(number, 3)); // Returns: 125
Root
To calculate the square root of a number, you can use the Math.sqrt() function:
const number = 25;
console.log(Math.sqrt(number)); // Returns: 5
This function, however, is only usable for calculating the square root.
To calculate another root, such as the cube root, use the ** operator or the Math.pow() function:
const number = 125;
console.log(number ** (1 / 3)); // Returns: 5
console.log(Math.pow(number, 1 / 3)); // Returns: 5