![]() |
VOOZH | about |
This article will demonstrate how to compute the iterative power of a number using JavaScript. It will include iterative methods to calculate and display the power of a given number.
Table of Content
In this method, we will use JavaScript for loop to iterate and calculate output by multiplication.
Example: In this example, we will calculate the output of 5 to power 3.
125
In this method, we will use JavaScript while loop to iterate and calculate output by multiplication.
Example: In this example, we will calculate the output of 7 to power 3.
343
In this method, we will use a recursive function to iterate and perform multiplication at every iteration.
Example: In this example, we will calculate 8 to the power 3 using recursion.
512
In this approach, we will use JavaScript array methods like array constructor, fill and reduce methods to calculate the power of the given number
// Creating array
arr = new Array(total elements).fill(value)
arr.reduce((accumulator , value)=> accumulator*value, initial value of accumulator)
Example: In this example, we will print the value of 2 to power 5
32
This approach iteratively computes the power of a number using bitwise operators. It continuously updates the result by squaring the base and dividing the exponent by 2. If the least significant bit of the exponent is 1, it multiplies the result by the current base value. This process continues until the exponent becomes zero.