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

// computes 52
let power = Math.pow(5, 2);
console.log(power); 

// Output:  25

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

// pow() with positive arguments let power1 = Math.pow(5, 3);
console.log(power1);
// pow() with negative arguments let power2 = Math.pow(-4, -2);
console.log(power2); // Output: // 125 // 80.44885596939699

Here,

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

Example 2: Math.pow() with Zero Arguments

// number with zero power let power1 = Math.pow(4, 0);
console.log(power2);
// zero raised to a positive power let power2 = Math.pow(0, 5);
console.log(power2);
// zero raised to negative power let power3 = Math.pow(0, -3);
console.log(power3); // Output: // 1 // 0 // Infinity

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

// power of floating point numbers let power1 = Math.pow(4.5, 2.3);
console.log(power1);
// power of negative number with floating point let power2 = Math.pow(-8, 4.3);
console.log(power2); // Output: // 31.7971929089206 // NaN

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

// pow() numeric string let power1 = Math.pow("6", "2");
console.log(power1); // Output: 36
// pow() with non-numeric string let power2 = Math.pow("Harry", "Potter");
console.log(power2); // Output: NaN

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:

Did you find this article helpful?