0
0
JavascriptProgramBeginner · 2 min read

JavaScript Program to Find Power Using Recursion

You can find power using recursion in JavaScript by defining a function like function power(base, exponent) { if (exponent === 0) return 1; return base * power(base, exponent - 1); } which multiplies the base by itself exponent times recursively.
📋

Examples

Inputpower(2, 3)
Output8
Inputpower(5, 0)
Output1
Inputpower(3, 1)
Output3
🧠

How to Think About It

To find the power of a number using recursion, think of the problem as multiplying the base number by itself repeatedly. The base case is when the exponent is zero, which always returns 1. For other cases, multiply the base by the result of the function called with the exponent decreased by one.
📐

Algorithm

1
Get the base and exponent as input.
2
Check if the exponent is zero; if yes, return 1.
3
Otherwise, multiply the base by the result of the function called with exponent minus one.
4
Return the result.
💻

Code

javascript
function power(base, exponent) {
  if (exponent === 0) {
    return 1;
  }
  return base * power(base, exponent - 1);
}

console.log(power(2, 3)); // Output: 8
console.log(power(5, 0)); // Output: 1
console.log(power(3, 1)); // Output: 3
Output
8 1 3
🔍

Dry Run

Let's trace power(2, 3) through the code

1

Initial call

power(2, 3) checks if exponent === 0 (false), returns 2 * power(2, 2)

2

Second call

power(2, 2) checks if exponent === 0 (false), returns 2 * power(2, 1)

3

Third call

power(2, 1) checks if exponent === 0 (false), returns 2 * power(2, 0)

4

Base case

power(2, 0) returns 1 because exponent === 0

5

Return values

power(2, 1) returns 2 * 1 = 2 power(2, 2) returns 2 * 2 = 4 power(2, 3) returns 2 * 4 = 8

CallExponentReturn Value
power(2, 0)01
power(2, 1)12
power(2, 2)24
power(2, 3)38
💡

Why This Works

Step 1: Base Case

The function stops calling itself when the exponent is zero by returning 1, because any number to the power of zero is 1.

Step 2: Recursive Step

For exponents greater than zero, the function multiplies the base by the result of the same function with the exponent decreased by one.

Step 3: Building the Result

Each recursive call waits for the smaller problem to solve, then multiplies the base, building up the final power value.

🔄

Alternative Approaches

Using a loop
javascript
function powerLoop(base, exponent) {
  let result = 1;
  for (let i = 0; i < exponent; i++) {
    result *= base;
  }
  return result;
}

console.log(powerLoop(2, 3)); // 8
This uses iteration instead of recursion, which can be more efficient and avoids call stack limits.
Using exponentiation operator
javascript
function powerOperator(base, exponent) {
  return base ** exponent;
}

console.log(powerOperator(2, 3)); // 8
This uses JavaScript's built-in operator for power, which is the simplest and fastest method.

Complexity: O(n) time, O(n) space

Time Complexity

The function calls itself once per decrement of the exponent, so it runs in linear time relative to the exponent.

Space Complexity

Each recursive call adds a new layer to the call stack, so space used is proportional to the exponent.

Which Approach is Fastest?

Using the built-in exponentiation operator is fastest and uses constant space, while recursion is easier to understand but uses more memory.

ApproachTimeSpaceBest For
RecursionO(n)O(n)Learning recursion and simple cases
LoopO(n)O(1)Avoiding call stack overhead
Exponentiation OperatorO(1)O(1)Fastest and simplest in JavaScript
💡
Always include a base case in recursion to avoid infinite calls.
⚠️
Forgetting to reduce the exponent in the recursive call causes infinite recursion.