javascript

JavaScript to find Power of a number without using pow() function

To find the Power of a number in JavaScript you can use the readily available function Math.pow().

But sometimes, especially in the interviews they might ask you to write a simple JavaScript program to calculate the power of a number without using the JavaScript pow() function.

Here is a small example of “Power of a number”

5= 5 * 5, value is 25

Here in the above expression example, 5 is the base & 2 is the exponent

Another example, 5= 5 * 5 * 5, value is 125

So basically we need get two values from the user, base & exponent then to find the power, just loop exponent times and multiple the base value.

Let’s write a custom JavaScript function to find the power of a number:

b and e is the base and exponent in the above function

 

Let’s write a HTML example to see the output:

See the demo of this above script here:

demo

Related Posts

Leave a Reply