• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / Using JavaScript to Count Even Numbers in an Array

Using JavaScript to Count Even Numbers in an Array

July 8, 2022 Leave a Comment

There are a couple of ways we can use JavaScript to count the even numbers in an array. One of the simplest ways is to just use a for loop to loop through each number in the array and count the even ones.

var count = 0;
for ( var i = 0; i<someArray.length; i++ ){ 
  if (someArray[i] % 2 == 0){
    count++;
};

Let’s put this code in a function to make it easier to count the even numbers in an array.

function evenNumbers(arr){
var count = 0;
  for ( var i = 0; i<arr.length; i++ ){ 
    if (arr[i] % 2 == 0){
      count++;
    }
  }
  return count;
};

Now let’s show some examples of this function in action.

function evenNumbers(arr){
var count = 0;
  for ( var i = 0; i<arr.length; i++ ){ 
    if (arr[i] % 2 == 0){
      count++;
    }
  }
  return count;
};

var array1 = [0,1,2,3,4,5,6,7,8,9];
var array2 = [1.2,3,4,-2,-4];
var array3 = [0,0,0,0];
var array4 = [-2,-4,-6,-8];
var array5 = [120,120,-120,-120,5,5];

console.log(evenNumbers(array1));
console.log(evenNumbers(array2));
console.log(evenNumbers(array3));
console.log(evenNumbers(array4));
console.log(evenNumbers(array5));

#Output
5
3
4
4
4

When working with collections of data, the ability to easily summarize and get statistics about the collection is valuable.

One such case is if you want to count the even numbers in an array.

To count the even numbers in an array in JavaScript, one of the easiest ways is with a for loop. To get the even numbers, we just need to check if the number is even or odd.

Once again is our simple function showing you how to count the number of even numbers in an array using JavaScript.

function evenNumbers(arr){
var count = 0;
  for ( var i = 0; i<arr.length; i++ ){ 
    if (arr[i] % 2 == 0){
      count++;
    }
  }
  return count;
};

Using the Filter Method to Count the Even Numbers in an Array

If we want an even simpler way to count the even numbers in an array, we can make use of the JavaScript filter() method.

var count = someArray.filter(num => {
  return num % 2 == 0;
});

The filter method above will return a new array of just even numbers, which will be assigned to the variable count.

We can then just use the length property on our new array to see how many even numbers are in our original array.

We’ll put this code in a function to make it very simple to get even numbers in an array.

function evenNumbers(arr){
  var count = arr.filter(num => {
    return num % 2 == 0;
  });
  return count.length;
};

Let’s test this function with our same array of numbers from above to see if we get the same result:

function evenNumbers(arr){
  var count = arr.filter(num => {
    return num % 2 == 0;
  });
  return count.length;
};

var array1 = [0,1,2,3,4,5,6,7,8,9];

console.log(evenNumbers(array1));

#Output
5

Hopefully this article has been useful for you to learn how to use JavaScript to count the even numbers in an array.

Other Articles You'll Also Like:

  • 1.  Using JavaScript to Check if Variable is a Function
  • 2.  How to Use JavaScript to Check if a String is Empty
  • 3.  How to Check if a String Contains Vowels in JavaScript
  • 4.  Using JavaScript to Return String
  • 5.  Remove the First and Last Character From a String in JavaScript
  • 6.  Convert String to Float in JavaScript
  • 7.  Swapping Images in JavaScript
  • 8.  Using JavaScript to Add Trailing Zeros
  • 9.  Math abs JavaScript – How to get the absolute value of a number using JavaScript
  • 10.  Adding an Underline with JavaScript

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy

x