JavaScript Math pow() method

The Math.pow function in JavaScript computes the result of raising a base to a specified exponent, which can be expressed as base^exponent. To clarify, the base value (x) is multiplied by itself for the total count indicated by the exponent (y). This method belongs to the Math object and is classified as a static method.

Syntax

The syntax for the Math.pow method is denoted as follows:

Example

Math.pow(base,exponent)

Parameter

Base - The number that is to be raised.

Exponent - The degree to which the base numeral is elevated.

Return

It provides the result of raising the base to the exponent's power.

Examples of JavaScript Math.pow method

In this section, we will explore the Math.pow function by examining a range of examples.

Example 1: Printing the power of a given number

Let’s consider a straightforward example that demonstrates how to calculate and display the power of a specified number.

Example

Example

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

console.log(Math.pow(5,1.4));

Output:

Output

8

9.518269693579391

Explanation:

In this illustration, we will examine the method of displaying the power of a specified number. In this case, we utilized Math.pow(2, 3), which computes 2 raised to the power of 3, and the result is then printed to the console. Consequently, the output produced is 8. In a similar fashion, we applied Math.pow(5, 1.4), where 5 is elevated to the power of 1.4, and the output is logged to the console. Thus, the resulting value is 9.518269693579391.

Example 2: Utilizing the Math.pow method with various inputs in JavaScript

Let us examine an illustration to grasp the functionality of the pow method through various test scenarios.

Example

Example

console.log(Math.pow(2,-3));  

console.log(Math.pow(-3,2));  

console.log(Math.pow(-5,1.4));  

console.log(Math.pow(5,-1.4));

Output:

Output

0.125

9

NaN

0.1050611121761507

Explanation:

In the following example, we will explore the application of the Math.pow method with different parameters in JavaScript. When we evaluate Math.pow(2,-3), we are calculating 2 raised to the power of -3. In this case, negative exponents indicate a reciprocal. This means we are performing the inverse operation of raising to a positive exponent, and when we log the output to the console, it results in 0.125. Next, we consider Math.pow(-3,2), which involves raising -3 to the power of 2. Here, the exponentiation of a negative number yields a positive outcome, as the product of two negative numbers is positive.

In the expression Math.pow(-5, 1.4), we are attempting to elevate -5 to the exponent of 1.4. This results in NaN (Not a Number) because 1.4 is a fractional exponent, indicating the necessity of a root operation. However, the Math.pow function does not accommodate complex numbers.

Example 3: Calculating power utilizing JavaScript's Math.pow

Consider a scenario in which we will compute power using the Math.pow function provided by JavaScript.

Example

Example

<!DOCTYPE html>

<html>

<head>

<title>Math.pow() Method</title>

</head>

<body>

<form>  

Base: <input type="text" id="base"><br><br>    

Exponent: <input type="text" id="exp"> <br><br>    

  <input type="button"  onclick="display()" value="submit">  

</form>  

<p><span id="result"></span></p>  



<script>  

function display()  

{  

  var x=document.getElementById("base").value;  

  var y=document.getElementById("exp").value;  

    

  document.getElementById("result").innerHTML=Math.pow(x,y);  

}  

</script>  

</body>

</html>

Output:

Upon inputting the base: 2 and the exponent: 4, the result we obtain is as follows:

Explanation:

In this illustration, we established a basic HTML form integrated with JavaScript capabilities, allowing users to input two values: a base and an exponent, into two separate text fields. Upon clicking the submit button, the result is displayed. We declared two variables, referred to as x and y, using the var keyword.

Upon clicking the button, the display function is activated, which collects the input values by referencing the IDs associated with the base and exponent elements. By employing Math.pow(x, y), the outcome of elevating the base to the exponent’s power is continuously refreshed within the content of the <span> element, identified by the id result, and is presented on the webpage.

Example 4: Fractional exponent utilizing the Math.pow method

Example

Example

// Using Math.pow() to calculate square root (exponent 1/2)

let number = 9;

let result = Math.pow(number, 1/2);

console.log(result); // Output: 3

Output:

Explanation:

In this instance, we will explore an illustration of a fractional exponent by employing the Math.pow function. We have instantiated a variable called number with the let keyword and assigned it the value of 9. The expression Math.pow(number, 1/2) is utilized, in which two arguments are provided, with the number being 9. Thus, we compute Math.pow(9, 1/2), indicating 9 raised to the power of 1/2, and this result is then output to the console, yielding a value of 3.

Conclusion

In JavaScript, the Math.pow function is employed to compute the power of a number, effectively raising a specified base to a given exponent. This function accepts two numerical parameters, which are the base and the exponent, and it returns the outcome of base^exponent. The method accommodates both integer and floating-point numbers, and it can process negative as well as fractional exponents. However, using a negative base with a fractional exponent may yield NaN. Additionally, the result will also be NaN if the provided arguments are not real numbers. This function proves to be extremely beneficial when carrying out exponential computations.

Frequently Asked Questions (FAQs):

  1. What is the purpose of Math.pow in JavaScript?

The function Math.pow serves the purpose of determining the result of a base number elevated to the power of a specified exponent. It accepts two parameters: the first is the base, which represents the number that will be elevated, and the second is the exponent, indicating the power to which the base will be raised.

  1. Is it possible to employ Math.pow using floating-point numbers as the exponent?

Indeed, we can make use of Math.pow with floating-point numbers as exponents. This functionality enables us to execute calculations such as square roots or cube roots.

  1. What will be the result if the inputs provided to Math.pow are not numerical values?

If either or both of the arguments are non-numeric and cannot be transformed into a number, the function will yield NaN.

  1. What will be the output of Math.pow(0, 0) in JavaScript?

In JavaScript, the expression Math.pow(0, 0) yields a value of 1.

  1. Here are several scenarios where Math.pow produces NaN:

There are certain scenarios where Math.pow yields NaN, including:

  • It will return NaN when the base is a negative number and the exponent is a non-integer, for instance, Math.pow(-4, 0.5)).
  • If either or both of the inputs are non-numeric and cannot be transformed into a number, then the function will also return NaN.

Input Required

This code uses input(). Please provide values below: