Count the True values in an Array using JavaScript

avatar

Borislav Hadzhiev

Sun Nov 14 20212 min read

banner

Photo by Frank Mckenna

Count the True values in an Array #

Use the filter() method to count the true values in a JavaScript array, e.g. arr.filter(value => value === true).length. The filter method will return an array containing the elements that satisfy the condition, on which we can access the length property.

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 all values that are equal to true.

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.

To get the total count of truthy values, we access the length property on the array, just like in the last example.

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 setting it 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 #

Join my newsletter

I'll send you 1 email a week with links to all of the articles I've written that week

Buy Me A Coffee