JavaScript Math pow()

The pow() method computes the power of a number by raising the second argument to the power of the first argument.

Example


pow() Syntax

The syntax of the Math.pow() method is:

Math.pow(number, power)

Here, pow() is a static method. Hence, we are accessing the method using the class name, Math.


pow() Parameters

The pow() method takes two parameters:

  • number - the base value that is raised to a certain power
  • power - the exponent value that raises number

pow() Return Value

The pow() method returns:

  • numberpower, number raised to a certain power
  • 1 if value of power is 0
  • 0 if value of number is 0

Note: If the argument is a non-numeric string, the result will be NaN (Not a Number).


Example 1: JavaScript Math.pow() with Integer Parameters

Here,

  • Math.pow(5, 3) - computes 53
  • Math.pow(-4, -2) - computes -4-2

Example 2: Math.pow() with Zero Arguments

Here,

  • Math.pow(4, 0) - computes 40 (equals to 1)
  • Math.pow(0, 5) - computes 05 (equals to 0)
  • Math.pow(0, -3) - computes 0-3 (equals Infinity)

Example 3: Math.pow() with Floating Point Parameters

The pow() method can calculate the power value when both arguments are positive floating point numbers as shown in the example above.

But if we use a floating point power argument with any type of negative number, the pow() method returns NaN as the output.


Example 4: Math.pow() with String Arguments

In the above example, we have used the pow() method with string arguments. Here,

  • Math.pow("6", "2") - converts the numeric string to numbers and computes the power
  • Math.pow("Harry", "Potter") - cannot compute the power of a non-numeric string and returns NaN

Recommended readings: