JavaScript Program to Find Sum of Even Numbers of an Array
Last Updated :
26 Feb, 2024
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);
OutputSum 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);
OutputSum 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);
OutputSum 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);
OutputSum of even numbers: 12
Similar Reads
JavaScript Program to Find Sum of Odd Numbers in an Array In JavaScript, working with arrays is a common task. Often one needs to manipulate the elements within an array, such as finding specific values or calculating their sum. Below are the approaches to find the sum of all odd numbers within an array: Table of Content 1. Using a loop2. Using the filter
4 min read
JavaScript Program to Count Even and Odd Numbers in an Array In this article, we will write a program to count Even and Odd numbers in an array in JavaScript. Even numbers are those numbers that can be written in the form of 2n, while Odd numbers are those numbers that can be written in the form of 2n+1 form. For finding out the Even and Odd numbers in an arr
4 min read
JavaScript Program to Print Even Numbers in a Linked List A linked list is a data structure that stores the values at different memory locations concerning the next memory block of stored value. You can get all the even numbers stored in a linked list using the below methods. Table of Content Using While LoopUsing RecursionUsing While LoopTo print even num
2 min read
Javascript Program for Frequencies of even and odd numbers in a matrix Given a matrix of order m*n then the task is to find the frequency of even and odd numbers in matrix.Examples: Input : m = 3, n = 3 { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 }Output : Frequency of odd number = 5 Frequency of even number = 4Input : m = 3, n = 3 { 10, 11, 12 }, { 13, 14, 15 }, { 16, 17, 18
3 min read
JavaScript program to print even numbers in an array Given an array of numbers and the task is to write a JavaScript program to print all even numbers in that array. We will use the following methods to find even numbers in an array: Table of Content Method 1: Using for Loop Method 2: Using while Loop Method 3: Using forEach LoopMethod 4: Using filter
5 min read
Sum of Squares of Even Numbers in an Array using JavaScript JavaScript program allows us to compute the sum of squares of even numbers within a given array. The task involves iterating through the array, identifying even numbers, squaring them, and summing up the squares. There are several approaches to find the sum of the square of even numbers in an array
2 min read
Java Program to Find the Sum of First N Odd & Even Numbers When any number which ends with 0,2,4,6,8 is divided by 2 that is an even number. And when any number ends with 1,3,5,7,9 is not divided by two is an odd number. Example: Input : 8 Output: Sum of First 8 Even numbers = 72 Sum of First 8 Odd numbers = 64Approach #1: Iterative Create two variables eve
3 min read
Print all Even Numbers in a Range in JavaScript Array We have to find all even numbers within a given range. To solve this question we are given the range(start, end) in which we have to find the answer. There are several ways to print all the even numbers in a range in an array using JavaScript which are as follows: Table of Content Using for Loop in
3 min read
Java Program to Compute the Sum of Numbers in a List Using Recursion ArrayList is a part of the Collection framework and is present in java.util package. It provides us with dynamic arrays in Java. Though, it may be slower than standard arrays but can be helpful in programs where lots of manipulation in the array is needed. This class is found in java.util package. I
5 min read
C# Program to Find the Index of Even Numbers using LINQ Given an array, now our task is to find the index value of the even numbers present in the given array using LINQ. LINQ is known as Language Integrated Query and was introduced in .NET 3.5. It gives the power to .NET languages to generate queries to retrieve data from the data source. So to do this
2 min read