How to get the sum of the powers of all numbers in JavaScript?

In this tutorial, we'll be discussing how to get the sum of the powers of all the numbers from start to end in JavaScript. We'll be using the built-in Math.pow() method to calculate the powers and the Array reduce() method to sum up the values.

Using the Math.pow() Method

The Math.pow() method allows us to calculate the power of a number. We can use this method to calculate the powers of all the numbers from start to end.

Syntax

Math.pow(base, exponent);

Parameters

  • Base ? The base number.

  • Exponent ? The exponent to which to raise the base.

Example

For example, if we want to get the sum of the power 2 of all the numbers from 1 to 4, we can do so by using the following code ?

<html>
<head>
   <title>Sum of Powers Example</title>
</head>
<body>
   <div id="result"></div>
   <script>
      var sum = 0;
      for(var i = 1; i <= 4; i++) {
         sum += Math.pow(i, 2);
      }
      document.getElementById("result").innerHTML = "Sum: " + sum;
   </script>
</body>
</html>
Sum: 30

In the code above, we first created a variable named sum and initialized it to 0. We then used a for loop to iterate through all the numbers from 1 to 4. For each number, we calculate its power (1² + 2² + 3² + 4² = 1 + 4 + 9 + 16 = 30) and add it to the sum.

Using the Array.reduce() Method

Another way to get the sum of the powers of all the numbers from start to end is to use the Array.reduce() method. The Array.reduce() method allows us to reduce an array to a single value by applying a function to each element.

Example

For example, if we want to get the sum of the power 3 of all the numbers from 1 to 4, we can do so by using the following code ?

<html>
<head>
   <title>Array.reduce() Method Example</title>
</head>
<body>
   <div id="result"></div>
   <script>
      var numbers = [1, 2, 3, 4];
      var sum = numbers.reduce(function(accumulator, current) {
         return accumulator + Math.pow(current, 3);
      }, 0);
      document.getElementById("result").innerHTML = "Sum: " + sum;
   </script>
</body>
</html>
Sum: 100

In the code above, we first created an array named numbers which contains all the numbers from 1 to 4. We then used the Array.reduce() method to sum up the cube values (1³ + 2³ + 3³ + 4³ = 1 + 8 + 27 + 64 = 100).

Using ES6 Arrow Function

We can also use the ES6 arrow function with reduce() for a more concise syntax to get the sum of the powers of all the numbers from start to end.

Example

For example, if we want to get the sum of the square powers of all the numbers from 1 to 4 using arrow function ?

<html>
<head>
   <title>ES6 Arrow Function Example</title>
</head>
<body>
   <div id="result"></div>
   <script>
      const numbers = [1, 2, 3, 4];
      const sum = numbers.reduce((accumulator, current) => accumulator + Math.pow(current, 2), 0);
      document.getElementById("result").innerHTML = "Sum: " + sum;
   </script>
</body>
</html>
Sum: 30

The arrow function provides a cleaner, more readable syntax while achieving the same result as the traditional function approach.

Creating a Reusable Function

Here's a practical function that calculates the sum of powers for any range:

<html>
<head>
   <title>Reusable Function Example</title>
</head>
<body>
   <div id="result"></div>
   <script>
      function sumOfPowers(start, end, exponent) {
         let sum = 0;
         for(let i = start; i <= end; i++) {
            sum += Math.pow(i, exponent);
         }
         return sum;
      }
      
      const result1 = sumOfPowers(1, 5, 2); // Sum of squares from 1 to 5
      const result2 = sumOfPowers(2, 4, 3); // Sum of cubes from 2 to 4
      
      document.getElementById("result").innerHTML = 
         "Sum of squares (1-5): " + result1 + "<br>" +
         "Sum of cubes (2-4): " + result2;
   </script>
</body>
</html>
Sum of squares (1-5): 55
Sum of cubes (2-4): 99

Conclusion

In this article, we've explored multiple approaches to calculate the sum of powers in JavaScript. The Math.pow() method with loops provides direct control, while Array.reduce() offers a functional programming approach that's particularly useful when working with existing arrays.

Updated on: 2026-03-15T23:19:00+05:30

613 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements