- “Math.pow()” Method
- “Exponentiation Operator (**)”
We will now go through each of the mentioned approaches one by one!
The Math.pow() method returns the value of “a” to the power of “b” like (a^b). It returns a “Nan” value in the case of a negative base and a non-integer base. You can also utilize this method to return the specified power of the provided number.
Here, “pow” refers to the power method, “base” refers to the number whose value is to raise, and “exponent” is the required value.
Go through the following example for a better understanding of the stated concept.
First, we will observe two different cases. First, we will display the base “7” with exponent value “2” which will work as “7^2=49”:
Now, we will verify the “NaN” condition by placing a negative base value, and a non-integer exponent value as follows:
The output of the above implementation is given below:
The Exponentiation operator (**) changes the value of the first variable with respect to the assigned second value which will act as power of the first variable. However, it also returns a “NaN” under the same circumstances discussed in the previous method. To utilize this operator, you can add (**) in between both operand values where the first value will act as a “base”, and the operand value after the exponentiation operator (**) will be raised according to the added base.
Here, “x” refers to the value or a variable, and “y” is the power value that needs to be applied at x.
Firstly, we will declare a variable named “base” and store the value “2” in it:
Next, we will raise the assigned value of the base variable to the power “3” using the exponentiation operator “**”. Here, in place of the base variable, you can also add any integer value directly:
The corresponding output, in this case, will be: