Wednesday, July 3, 2024
HomeLanguagesJavascriptHow to find the sum of all elements of a given array...

How to find the sum of all elements of a given array in JavaScript ?

In this article, we will learn how we can find the sum of all elements/numbers of the given array. There are many approaches to solving the problems using for loop, forEach() method, and reduce() method.

Below all the approaches are described with a proper example:

Method 1: Using for loop

We are simply going to iterate over all the elements of the array using a Javascript for loop to find the sum.

Example: This example shows the above-explained approach.

Javascript




// Creating array
let arr = [4, 8, 7, 13, 12]
 
// Creating variable to store the sum
let sum = 0;
 
// Running the for loop
for (let i = 0; i < arr.length; i++) {
    sum += arr[i];
}
 
console.log("Sum is " + sum) // Prints: 44


Output

Sum is 44

Method 2: Using forEach() Method

We are going to use the Javascript forEach() method of the array to calculate the sum.

Example: This example shows the above-explained approach.

Javascript




// Creating array
let arr = [4, 8, 7, 13, 12]
 
// Creating variable to store the sum
let sum = 0;
 
// Calculation the sum using forEach
arr.forEach(x => {
    sum += x;
});
 
// Prints: 44
console.log("Sum is " + sum);


Output

Sum is 44

Method 3: Using reduce() Method

We are going to use the Javascript reduce() method to find the sum of the array.

Example: This example shows the above-explained approach.

Javascript




// Creating array
let arr = [4, 8, 7, 13, 12]
 
// Using reduce function to find the sum
let sum = arr.reduce(function (x, y) {
    return x + y;
}, 0);
 
// Prints: 44
console.log("Sum using Reduce method: " + sum);


Output

Sum using Reduce method: 44

Method 4: Using Recursion

We could define recursion formally in simple words, that is, a function calling itself again and again until it doesn’t have left with it anymore.

Example: This example shows the above-explained approach.

Javascript




// Creating array
let arr = [4, 8, 7, 13, 12];
 
// Function to find the sum of the array using recursion
function sumArray(arr, index) {
    if (index === arr.length) {
        return 0;
    }
    return arr[index] + sumArray(arr, index + 1);
}
 
console.log("Sum is " + sumArray(arr, 0));


Output

Sum is 44

Nicole Veronica Rubhabha
Nicole Veronica Rubhabha
A highly competent and organized individual DotNet developer with a track record of architecting and developing web client-server applications. Recognized as a personable, dedicated performer who demonstrates innovation, communication, and teamwork to ensure quality and timely project completion. Expertise in C#, ASP.Net, MVC, LINQ, EF 6, Web Services, SQL Server, MySql, Web development,
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments