JavaScript

# How to Use Exponents in JavaScript

In JavaScript, we often come across solving Math problems, including complex equations, taking measurements, and calculating multi-dimensional quantities. For instance, instead of writing out “x” multiplied by itself two times, we can simply write “x^2”, which makes calculation easier. In such cases, using exponents becomes very handy. Moreover, utilizing exponents also allows us to abbreviate something that would otherwise be time-consuming to think about.

## How to Use/Apply Exponents in JavaScript?

To utilize Exponents in JavaScript, you can use:

• Math.pow()” Method
• Exponentiation Operator (**)

We will now go through each of the mentioned approaches one by one!

## Method 1: Use Exponents in JavaScript Using Math.pow() Method

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.

Syntax

Math.pow(base, exponent)

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.

Example
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”:

console.log(Math.pow(7, 2));

Now, we will verify the “NaN” condition by placing a negative base value, and a non-integer exponent value as follows:

console.log(Math.pow(-7, 0.5));

The output of the above implementation is given below:

## Method 2: Use Exponents in JavaScript Using Exponentiation Operator(**)

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.

Syntax

x ** y

Here, “x” refers to the value or a variable, and “y” is the power value that needs to be applied at x.

Example
Firstly, we will declare a variable named “base” and store the value “2” in it:

let base= 2

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:

console.log(base ** 3)

The corresponding output, in this case, will be:

We have provided the simplest methods to use exponents in JavaScript.

## Conclusion

To use Exponents in JavaScript, you can utilize the “Math.pow()” method for assigning an exponent power to a base value and “Exponentiation Operator(**)” for raising the base value(on the left) to the provided exponent value(on the right) separated by (**). This write-up has explained the methods to use exponents in JavaScript.