JavaScript provides a wide range of functions for fast and simple use of mathematical expressions. One of the most frequently used functions is Math.pow(). It allows you to calculate the power of a number.

The Math.pow() function accepts two obligatory arguments. The first parameter is a base - the number for which you need the power to be computed. The second one is a number specifying the exponent. For example, if you call Math.pow (4, 3), the function will return the result of computing 4*4*4 which is 64. 

Note that this function is a static one. That’s why you should not try to create an object of Math in order to call it (Math does not have a constructor by the way). The correct way to use the function is directly by calling Math.pow (base, exponent) as shown in the example below.

console.log(Math.pow(3, 3)); // logs 27

The function works perfectly fine with both positive and negative numbers as a base. It can also accept numbers with a floating point, numbers written in a scientific (exponential) form or even numerical strings. 

The Math.pow() function can recognize numbers represented in a hexadecimal system but the returned result would be written in decimal. All these cases are illustrated in the following example.  

console.log(Math.pow(2.5, 2)); // logs 6.25
console.log(Math.pow('-1.5', '3')); // logs -3.375
console.log(Math.pow(2e21, 2)); // logs 4e+42
console.log(Math.pow(0x1b, 1/3)); // logs 3

Keep in mind that taking 0 as an exponent will always return 1 in a result. Passing non-numeric values to this function will lead to NaN returned as a result.

Computing roots with Math.pow()

You can use this function for calculating square roots and other radicals by passing a fractional number as the argument for exponent. In order to understand how to choose the correct exponent for calculating certain roots you need to dig a bit deeper into Mathematics.

Any fractional number consists of a numerator (which is above the line) and a denominator (the number below the line). While using fractions as exponents, just note that the numerator corresponds to a power and a denominator goes for a root. 

You can make your program calculate the square root simply by calling Math.pow(4, 0.5), Math.pow(4, 1/2) or Math.sqrt(4) - calculating square root. These two formats will lead to the same result.

console.log(Math.pow(4, 0.5)); // logs 2
console.log(Math.pow(4, 1/2)); // logs 2
console.log(Math.sqrt(4)); // logs 2

Keep in mind that according to a mathematical law, when calculating an even root the base cannot be negative. Hence, if you try to call the Math.pow() function with the first parameter less than zero and the second argument represented by a fractional number, NaN will be returned as a result. 

console.log(Math.pow(-16, 1/2)); // logs NaN
console.log(Math.pow(-16, 1/4)); // logs NaN

Even though Mathematics allows calculating odd roots of negative numbers, the Math.pow() function is not capable of that. For example, the result of execution Math.pow (-27, 1/3) will be NaN, although after manual calculation you will get -3

There are two ways to resolve this issue. First of all, you can use a built-in function Math.cbrt() for fast calculation of a cube root. 

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

The second way is a bit more complicated but also more general. That is creating your own function. The use of a ternary operator can be very helpful for this purpose. 

For calculating a cube root of a number you can make a function accepting just one parameter - the base. Then check whether it’s positive or negative. If the number is more than zero or equal to it, just return the result of the Math.pow(), otherwise - call the same function but with an opposite argument and then multiply the result by -1.

function cube_root(x) {
return x >= 0 ? Math.pow(x, 1/3): - Math.pow(-x,1/3);
}
console.log(cube_root(-27)); // logs -3

You can make the function more universal by passing two arguments instead of one and verifying them inside.

Math.pow() with negative exponents

In Mathematics the exponent may have a negative value as well. In this case it shows how many times you need to divide 1 by the specified number. For example, calculating 4-2 is the same as 1/42. The Math.pow() function is also able to perform such calculations. Thus, for this example it will return 0.0625 as a result.

console.log(Math.pow(4, -2)); // logs 0.0625

Moreover, Math.pow() can easily deal with negative fractional exponents. For example, if you take -1/2 as an exponent and 16 as a base to this function, it will return the result of division 1 by a square root of 16 that is 0.25.

console.log(Math.pow(16, -1/2)); // logs 0.25

Also, there might be some situations when the function returns a quite confusing result. For example, if you try to calculate 10 to a minus fifth power, instead of the expected 0.00001 the Math.pow() function will return a weird number 0.000009999999999999999. 

The reason for that is the way how JavaScript stores numbers (using the IEE755 standard). That’s why in some cases you might want to use functions for rounding additionally.

console.log(Math.pow(10, -5)); // logs 0.000009999999999999999

ES7 Exponentiation operator

One of the minor features introduced in EcmaScript2017(ES7) is the exponentiation operator.

The exponentiation operator (**) is equivalent to Math.pow() and returns the result of raising the first operand to the power of the second operand. The main difference - it also accepts BigInts as operands.

console.log(Math.pow(Math.pow(2, 3), 2)); // Output: 64
console.log((2 3) 2); // Output: 64

Conclusion

The Math.pow() function is a very powerful tool for calculating the power of the number. It can work with positive, negative and fractional exponents, which makes it very flexible. However, it’s not capable of calculating roots of negative numbers, so you might need to create your own function for that purpose or use the built-in Math.cbrt() function for computing a cube root.