Open In App

JavaScript Program to Find Sum of Even Numbers of an Array

Last Updated : 26 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In JavaScript, working with arrays is a basic operation. We have to sum all the even numbers present in the array. We can check the number if it is even or not by the use of the % operator.

These are the following ways to find the sum of Even numbers in an Array:

Sum of Even Numbers of an Array using Iterative Approach

This method iterates through each element in the array and checks if it’s even. If it is, the element is added to a running total variable, which stores the cumulative sum of all even numbers encountered so far.

Example: The function `sumOfEvenNumbers` calculates the sum of even numbers in an array using a loop and conditional statements.

Javascript




function sumOfEvenNumbers(arr) {
    let sum = 0;
    for (let i = 0; i < arr.length; i++) {
        if (arr[i] % 2 === 0) {
            sum += arr[i];
        }
    }
    return sum;
}
 
const numbers = [1, 2, 3, 4, 5];
const evenSum = sumOfEvenNumbers(numbers);
console.log("Sum of even numbers:", evenSum);


Output

Sum of even numbers: 6

Sum of Even Numbers of an Array using filter() and reduce() methods

This method provides a simpler answer by using built-in array functions. By using the filter method, an array is created that is limited to the even elements present in the original array. The filtered array is then iterated by using the reduce method, which adds each element to the sum.

Example: The `sumOfEvenNumbers` function filters the even numbers from the input array using the `filter` method and then calculates their sum using the `reduce` method.

Javascript




function sumOfEvenNumbers(arr) {
    return arr.filter(num => num % 2 === 0)
        .reduce((acc, num) => acc + num, 0);
}
 
const numbers = [1, 2, 3, 4, 5];
const evenSum = sumOfEvenNumbers(numbers);
console.log("Sum of even numbers:", evenSum);


Output

Sum of even numbers: 6

Sum of Even Numbers of an Array using Recursive Approach

This method solves the problem by using recursion. It defines a function that accepts three parameters: the array, the current index, and the sum of the total. After checking that the array has ended in the base case, the function calls itself recursively with the next element and, if the current number is even, changes the total.

Example: Recursive function `sumOfEvenNumbers` computes the sum of even numbers in an array. It recursively processes the array, adding even numbers to the sum. The base case returns 0 if the array is empty.

Javascript




function sumOfEvenNumbers(arr) {
    // Base case: If the array is empty, return 0.
    if (arr.length === 0) {
        return 0;
    }
 
 
    const firstElement = arr[0];
    const restOfArray = arr.slice(1);
 
    if (firstElement % 2 === 0) {
        return firstElement +
            sumOfEvenNumbers(restOfArray);
    } else {
 
        return sumOfEvenNumbers(restOfArray);
    }
}
 
const numbers = [1, 2, 3, 4, 5, 6];
const evenSum = sumOfEvenNumbers(numbers);
console.log("Sum of even numbers:", evenSum);


Output

Sum of even numbers: 12

Sum of Even Numbers of an Array using Using forEach Loop

This method uses forEach to iterate through the array, providing an alternative to the normal loop. It keeps readability and efficiency while providing a slightly more simple solution.

Example: The `sumOfEvenNumbers` function calculates the sum of even numbers in an array using a forEach loop. It iterates through the array, adding each even number to the sum. The example demonstrates its usage by finding the sum of even numbers in an array and printing the result.

Javascript




function sumOfEvenNumbers(arr) {
    let sum = 0;
 
    arr.forEach(num => {
        // Check if the current element is even.
        if (num % 2 === 0) {
            // If the current element
            // is even, add it to the sum.
            sum += num;
        }
    });
 
    // Return the total sum of even numbers.
    return sum;
}
 
const numbers = [1, 2, 3, 4, 5, 6];
const evenSum = sumOfEvenNumbers(numbers);
console.log("Sum of even numbers:", evenSum);


Output

Sum of even numbers: 12


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads