JavaScript Program to Compute Iterative Power of a Number

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.

Methods to Compute the Iterative Power of a Number

Table of Content

  • Method 1: Using JavaScript for Loop
  • Method 2: Using JavaScript while Loop
  • Method 3: Using Recursion
  • Method 4: Using the JavaScript array methods
  • Method 5: Using bitwise operators

Method 1: Using JavaScript for Loop

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.

Javascript
// Base number input
let n = 5

// Power input
let power = 3

// Result variable
let num = 1;
for(let i = 0; i < power; ++i){
      num = num * n; 
}

// Display output
console.log(num);

Output
125

Method 2: Using JavaScript while Loop

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.

Javascript
// Base number input
let n = 7

// Power input
let power = 3

// Result variable
let num = 1;
while (power) {
    num = num * n;
    power -= 1;
}

// Display output
console.log(num);

Output
343

Method 3: Using Recursion

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.

Javascript
// Recursive function to compute Power
function pow(n, p) {
    if (p == 1) return n;
    return n * pow(n, p - 1);
}

// Base number input
let n = 8

// Power input
let power = 3

// Display output
console.log(pow(n, power));

Output
512

Method 4: Using the JavaScript array methods

In this approach, we will use JavaScript array methods like array constructor, fill and reduce methods to calculate the power of the given number

Syntax:

// 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

Javascript
// Base number input
let n = 2;

// Power input
let power = 5;

// Creating new array having value n
let numArray = new Array(power).fill(n);

// Calculate the result output
let result = numArray.reduce((res, n) => (res *= n), 1);

// Display output
console.log(result);

Output
32

Method 5: Using bitwise operators

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.

JavaScript
function iterativePowerUsingBitwise(base, exponent) {
    let result = 1;

    while (exponent > 0) {
        if (exponent & 1) {
            result *= base;
        }
        base *= base;
        exponent >>= 1;
    }

    return result;
}

// Example usage
const base = 3;
const exponent = 4;
console.log(iterativePowerUsingBitwise(base, exponent)); // Output: 81