Write a number array and using for loop add only even numbers in javascript?


An array in JavaScript is an datatype which contains similar types of data. A number array is defined as the array object with numbers as its content. These numbers are divided into even and odd types on a basic level of mathematics. In this article, we will look into outputting the even number of any number array.

Syntax

let arr = [val1,val2,val3,…];

Now, let us consider an array arr with numbered elements and print the content in the 0th index −

Example

<!DOCTYPE html> <html lang="en"> <head> <script> let arr = [1,2,3,4,5]; document.write(arr[0]); </script> </head> <body> <html>

As we know, even numbers are those numbers that are completely divisible by 2. Or in technical words, we can say that mod (%) of any number with 2 should be (== 0).

Code Snippet −

Syntax

if(let num %2 == 0)
   Output the num

Using for loop

The iterative statements are used to visit all the elements in the array using the array indexing. We will use for loop to traverse through the array and print the even elements.

Example

In the program below, we will use iterative statements to compare every number in the number array and check if mod (%) of each element is zero. If yes, the element is printed in the output; if not, it is going to be omitted.

<!DOCTYPE html> <html lang="en"> <head> <title>Sum of Even Numbers</title> </head> <body> <script> var arr = [1,2,3,4,5,6,7,8,9,10,12]; var sum = 0; let i = 0; document.write("Even numbers are present in an array is = ") for(let i = 0; i<arr.length; i++){ if( arr[i] %2 == 0){ sum = sum + arr[i]; document.write(arr[i] + " "); } } document.write("<br>Sum of Even numbers = " + sum); </script> </body> </html>

Using while loop

We will use the while iterative statement in this example to traverse through the elements.

Example

In the example program below, we have defined an array with some elements. Using a while loop for fetching all the array elements, we introduced the if condition to checking the number is even or not. Finally, we have added all the even numbers.

<!DOCTYPE html> <html lang="en"> <head> <title>Sum of Even Numbers</title> </head> <body> <script> var arr = [1,2,3,4,5,20,7,8,9,10,12]; var e_sum = 0; let i = 0; document.write("Even numbers are present in an array is = ") while(i<arr.length){ if(!(arr[i] & 1 != 0)){ e_sum = e_sum + arr[i]; document.write(arr[i] + " "); } i++; } document.write("<br>Sum of Even numbers = " + e_sum); </script> </body> </html>

using map() method

map () method in JavaScript creates an array by calling a specific function. The conditions in the function can be executed in the two ways. The map() method will neither work on an empty array nor change the original array in any way.

Syntax

let arr = [val1, val2,…];
arr.map(condition)

(or)

let arr = [val1,val2…];
arr.map(function(val)
)};

map() method is a built-in method in JavaScript.

Let’s discuss it with a suitable example.

Example

In the program below, we have used the map() method; it creates an array by calling a specific function. We call a function with a parameter that holds the value of an array and finally, we perform the if condition for checking whether the number is even or not. We then calculate the sum of all the even numbers found and print the sum.

<!DOCTYPE html> <html lang="en"> <head> <title>Sum of an even numbers</title> </head> <body> <script> var arr = [1,2,3,4,5,6,7,8,9,10]; var sum = 0; document.write("Array Even elements = "); arr.map(function(ele){ if(ele %2 == 0){ document.write(ele + " "); sum = sum + ele; } }); document.write("<br>Sum of the even elements = "+ sum); </script> </body> </html>

Using forEach() loop

forEach() behaves the same as a map() method in JavaScript but the difference is that forEach() does not return an array (or anything, for that matter). forEach() method calls a function for each element of an array.

Syntax

let arr = [val1,val2,….];
arr.forEach(function(val))};

Example

In this program we are going to use forEach() loop in JavaScript to fetch all the array elements. Basically, the foreach() loop fetches all the array elements with some specific function. After that, we use conditional statements to check number is even or odd.

<!DOCTYPE html> <html lang="en"> <head> <title>Sum of an even numbers</title> </head> <body> <script> var arr = [2,3,4,20,6,7,8,9,10]; var sum = 0; document.write("Even numbers in an array = "); arr.forEach(function(ele){ if(ele %2 == 0){ document.write(ele + " "); sum += ele; } }); document.write("<br>Sum of an even numbers = " + sum); </script> </body> </html>

Updated on: 29-Aug-2022

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements