Saturday, October 18, 2025
HomeLanguagesJavascriptJavaScript Program to Compute Power of a Number

JavaScript Program to Compute Power of a Number

In this article, we will demonstrate different approaches for computing the Power of a Number using JavaScript.

The Power of a Number can be computed by raising a number to a certain exponent. It can be denoted using a^b, where the ‘a’ represents the base number & the ‘b’ represents the number to which the power is to be raised. These programs will have input numbers and power and will show the resulting output. There are various techniques to calculate the power of a number, which are described below with their illustrations.

Using JavaScript Loops

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

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

Using the Math.pow() Method

This is another method to method that is used to power a number i.e., the value of the number raised to some exponent. Here, we will use this method to calculate the power of the number.

Syntax

Math.pow(base, exponent);

Example: In this example, we will calculate 7 to power 9 using Math.pow() method.

Javascript




// Base number input
let n = 7;
let power = 9;
  
// Calculate and display output
console.log(Math.pow(n,power));


Output

40353607

Using JavaScript Exponentiation (**) Operator

This method can also be utilized to find the power of the first operator raised to the second operator, & it is denoted by a double asterisk(**) symbol.

Syntax

Base**Power

Example: In this example, we will use the JavaScript ** operator to get 17 to power 3.

Javascript




// Base number input
let n = 17;
let power = 3;
  
// Calculate and display output
console.log(n**power);


Output

4913
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS