Count the True values in an Array using JavaScript

avatar

Borislav Hadzhiev

Last updated: Jul 23, 2022

banner

Photo from Unsplash

Count the True values in an Array #

To count the true values in an array:

  1. Use the filter() method to iterate over the array.
  2. Check if each value is equal to true and return the result.
  3. Access the length property on the array to get the count of the true values.
index.js
// ✅ Count values equal to `true` const arr1 = [true, true, false, true]; const count1 = arr1.filter(value => value === true).length; console.log(count1); // 👉️ 3 // ✅ Count truthy values const arr2 = ['hi', 1, false, true, []]; const count2 = arr2.filter(Boolean).length; console.log(count2); // 👉️ 4

The function we passed to the Array.filter method gets called with each element in the array.

On each iteration, we check if the value is equal to true and return the result.

The array that the filter method returns will only contain the elements that satisfy the condition, in other words an array of only true values.

To get the count of true values, we access the length property on the array.

There is a distinction between getting the count of values that are equal to true and the truthy values in an array.

The truthy values are all values that are not falsy. The falsy values in JavaScript are: false, null, undefined, 0, "" (empty string), NaN (not a number).

To get the count of the truthy values in an array, call the filter method, passing it the Boolean object as a parameter and access the length property on the result, e.g. arr.filter(Boolean).length.

index.js
// ✅ Count truthy values const arr2 = ['hi', 1, false, true, []]; const count2 = arr2.filter(Boolean).length; console.log(count2); // 👉️ 4
The Boolean object gets passed each value from the original array, converts it to its boolean representation and returns the result.

The array the filter method returns contains all of the truthy values from the original array.

We accessed the length property on the array to get the total count of truthy values.

Alternatively, you can use the Array.reduce method.

index.js
const arr2 = ['hi', 1, false, true, []]; const count3 = arr2.reduce((accumulator, value) => { if (value) { return accumulator + 1; } return accumulator; }, 0); console.log(count3); // 👉️ 4

The function we passed to the reduce() method gets called for each element of the array.

We initialized the accumulator variable to 0. If the value of the current iteration is truthy, we return the value of the accumulator + 1, otherwise we return the accumulator as is.

The value we return from the callback function gets passed to the reduce method as the accumulator for the next iteration.

Once the method has finished iterating over the array, we have the total count of truthy elements in the array.

Further Reading #

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.