How to get the exponent power of a number in JavaScript?

JavascriptWeb DevelopmentFront End Technology

In this tutorial, we will learn how to get the exponent power of a number in JavaScript. The power of a number can be termed as how many times a particular number is multiplied by the number itself. For calculating the power of a number, we require two things, Base and Exponent. At the same time, a Base is a number that must be multiplied. Exponent (Usually written in the superscript) determines the number of times the number (Base) should be multiplied by itself.

While we represent the power of a number using "^" or superscript when typing it, we take the help of functions to compute the exponent power in JavaScript. Here we will cover two approaches to computing the exponent power of a number. The first approach is to use the Math.pow() method and the second is to use the exponentiation operator.

Using the Math.pow() Method

The Math.pow() is a static method, which means it is a member of an object which doesn't need a constructor to create an instance. It accepts two parameters, Base and the Exponent, and returns a number(Data type) which is the solution. The result is NaN if the Base is negative and the exponent is not an integer.

Syntax

Following is the syntax to get the exponent power of a number using the Math.pow() method −

Math.pow(base, exponent);

Parameter Details

  • base − the number that needs to be multiplied

  • exponent − The number of times a number needs to be multiplied

Example

In the below given example, we have selected two numbers, Base and Exponent, and we are finding out the Base's power using the Math pow() method.

Case1 - Base and Exponent are numbers

Case 2 -  A base is a number, and an exponent is a Decimal

<html> <body> <h3>Get the exponent power of a number using <i>Math.pow()</i> method</h3> <div id = "str1"></div> <script> var Base = 2; var Exponent = 5; var answer = Math.pow(Base, Exponent); var output = document.getElementById("str1"); output.innerHTML += " Case 1: Base = 'number', Exponent = 'number'<br/>" output.innerHTML += "Base = " + Base + "<br/>"; output.innerHTML += "Exponent = " + Exponent + "<br/>"; output.innerHTML += Base + " power " + Exponent + " = " + answer + "<br/> <hr>" Base = 2; Exponent = 0.5; answer = Math.pow(Base, Exponent); output.innerHTML += " Case 2: Base = 'number', Exponent = 'Decimal'<br/>" output.innerHTML += "Base = " + Base + "<br/>"; output.innerHTML += "Exponent = " + Exponent + "<br/>"; output.innerHTML += Base + " power " + Exponent + " = " + answer + "<br/><hr>" </script> </body> </html>

Example

In this example, we select two numbers: Base and Exponent Are Negative numbers.

Case 1 - A base is a number, and an exponent is a Negative number

Case 2 - A base is a Negative number, and an exponent is a Negative Number

<html> <body> <h3>Get the exponent power of a number using <i>Math.pow()</i>method</h3> <div id = "str1"></div> <script> var Base = 2; var Exponent = -5; var answer = Math.pow(Base, Exponent); var output = document.getElementById("str1"); answer = Math.pow(Base, Exponent); output.innerHTML += " Case 1: Base = 'number', Exponent = 'Negative Number'<br/>" output.innerHTML += "Base = " + Base + "<br/>"; output.innerHTML += "Exponent = " + Exponent + "<br/>"; output.innerHTML += Base + " power " + Exponent + " = " + answer + "<br/><hr>" Base = -2; Exponent = 0.5; answer = Math.pow(Base, Exponent); output.innerHTML += " Case 2: Base = 'Negative Number', Exponent = 'Negative Number'<br/>" output.innerHTML += "Base = " + Base + "<br/>"; output.innerHTML += "Exponent = " + Exponent + "<br/>"; output.innerHTML += Base + " power " + Exponent + " = " + answer + "<br/><hr>" </script> </body> </html>

This is how the Math.pow() function works and follows the basic mathematic rules.

Using the Exponentiation Operator

The exponentiation operator (**) can also be used to get the exponent power of a number. It returns a number which is the result of raising the first operand to the power of the second operand. It is the same as the Math.pow() method, discussed above. The only difference between these two methods is that the exponentiation operator can accept BigInts as operands.

Syntax

Following is the syntax to find the exponent power of a number using the exponentiation operator −

Operand1 ** Operand2

Here operand1 and operand2 are the first and second operands.

Please note if the first operand is negative, then we should put it in a parenthesis.

Example

In the program below, we take different types of numbers for the two operands.

<html> <head> <title>JavaScript Math exp() Method</title> </head> <body> <h3>Using the Exponentiation Operator</h3> <p id ="result"></p> <script> var value1 = 7 ** 2; document.getElementById("result").innerHTML = "7 ** 2 = " + value1; var value2 = (-8) ** 5; document.getElementById("result").innerHTML +="<br>(-8) ** 5 = " + value2; var value3 = 4 ** -3 document.getElementById("result").innerHTML +="<br>4 ** -3 = " + value3; var value4 = (-3) ** -4 document.getElementById("result").innerHTML +="<br>(-3) ** -4 = " + value4; </script> </body> </html>

In this tutorial, we have discussed two approaches to finding the exponent power of a number in JavaScript. The first approach is to use the Math.pow() method and the second is to use the exponentiation operator (**). The second method can accept BigInts whereas the first cannot.

raja
Updated on 26-Aug-2022 13:04:14

Advertisements