Categories
JavaScript Answers

How to count the number of true values in an array of boolean values with JavaScript?

Spread the love

To count the number of true values in an array of boolean values with JavaScript, we call the filter method.

For instance, we write

const arr = [true, false, true, false, true];
const count = arr.filter(Boolean).length;

to call arr.filter with Boolean to return an array with the truthy values in arr in a new array.

Then we get the count of that with the length property.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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