How to Use the Math.pow() Function in JavaScript?

Estimated read time 1 min read

The Math.pow() function in JavaScript is used to calculate the power of a number. It takes two arguments: the base number and the exponent. The function returns the result of raising the base to the power of the exponent.

Here’s an example that uses the Math.pow() function:

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

In the example above, Math.pow(2, 3) returns 8, which is the result of raising 2 to the power of 3.

You can also use the Math.pow() function with decimal values:

console.log(Math.pow(2.5, 3)); // Output: 15.625

In this example, Math.pow(2.5, 3) returns 15.625, which is the result of raising 2.5 to the power of 3.

Note that Math.pow() can return NaN (Not a Number) if either the base or the exponent is negative and the base is zero:

console.log(Math.pow(0, -2)); // Output: NaN

In this example, Math.pow(0, -2) returns NaN because the base is 0 and the exponent is negative.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply