Math.pow(x, y) works fine for x^y and even evaluates the expression when y is not an integer. A piece of code not relying on Math.pow but that can only evaluate integer exponents is:
function exp(base, exponent) {
exponent = Math.round(exponent);
if (exponent == 0) {
return 1;
}
if (exponent < 0) {
return 1 / exp(base, -exponent);
}
if (exponent > 0) {
return base * exp(base, exponent - 1)
}
}
How we perform exponents in JavaScript According to MDN
The exponentiation operator returns the result of raising the first operand to the power second operand. That is, var1 var2, in the preceding statement, where var1 and var2 are variables. Exponentiation operator is right associative: a ** b ** c is equal to a ** (b ** c).
For example: 2**3 // here 2 will multiply 3 times by 2 and the result will be 8. 4**4 // here 4 will multiply 4 times by 4 and the result will be 256.