Count The True Values In An Array In JavaScript

To count the true values in an array in JavaScript –

  1. Use the Array.filter() method with a function to iterate over the array.
  2. In each iteration of the function, check `arr.filter(value=> value === true)` if the value is true.
  3. The Array.filter() method creates a new array with all the elements that have `true` values.
  4. Find the length of this new array by using the Array.length property.

Let’s discuss this in detail below.

count the true values in an array in JavaScript

Count The True Values In An Array In JavaScript

We’ll be using the following array for our examples:

var arr = [“a”, “b”, true, false, 1, 0];

Let’s look at the code to get all the true values in an array –

var arr = [“a”, “b”, true, false, 1, 0];

var trueArray = arr.filter(val => val === true);

var trueCount = trueArray.length;

console.log(trueCount); //1

The Array.filter() method creates a new array with all the elements that pass the condition we provide in the function. So, if we pass in a function that always returns true – `arr.filter(()=>true)`, we get a new array with all the elements of the original array.

Similarly, `arr.filter(value=> value === true)` creates a new array with only those elements which are equal to true in the original array. So, the resultant array would be:

var trueArray = [true];

Now, we can use the Array.length property to get the number of elements in this array:

trueArray.length; // 1

Thus, the total number of true values in the original array is 1.

Note that there is a difference between getting `true` values and truthy values. Truthy values are all values that are not falsy. Falsy values in JavaScript are – false, 0, “”, null, undefined, and NaN.

Thus, if we want to get all the truthy values from an array in JavaScript, we can use the Array.filter() method with a Boolean function instead. For example –

var arr = [“a”, “b”, true, false, 1, 0];

var truthyArray = arr.filter(Boolean);

console.log(truthyArray); //[“a”, “b”, true, 1]

var truthyCount = truthyArray.length;

console.log(truthyCount); //4

As you can see from the output, the resultant array contains all truthy values from the original array. The Boolean function returns true for all truthy values and false for falsy values. So, only those elements which are truthy are added to the new array.

Similarly, we can also use the Array.reduce() method to get the count of true values in an array. The Array.reduce() method executes a function on each element of the array and returns a single value. So, we can use this method to get the count of true values in an array by providing a function that increments the counter variable for every true value it encounters.

For example –

var arr = [“a”, “b”, true, false, 1, 0];

var trueCount = arr.reduce((counter, value) => {

if(value === true){

  return counter+1;

}

return counter;

},0); //1

console.log(trueCount); //1

You can use the reduce() method to count the truthy values in an array as well. For that, you can use a Boolean function as the reducer function. The Boolean function returns 1 for all truthy values and 0 for falsy values. Thus, the counter variable gets incremented for every truthy value in the array.

For example –

var arr = [“a”, “b”, true, false, 1, 0];

var truthyCount = arr.reduce((counter, value) => {

return Boolean(value)? counter+1: counter;

},0); //4

console.log(truthyCount); //4

As you can see from the output, this code returns 4 – which is the total number of truthy values in the array.

You can also use the Array.forEach() method to get the count of true values in an array. The forEach() method executes a function on each element of the array. So, we can use this method to get the count of true values by incrementing a counter variable for every true value we encounter.

For example –

var arr = [“a”, “b”, true, false, 1, 0];

var trueCount = 0;

arr.forEach(value => {

if(value === true){

  trueCount++;

}

}); //1

console.log(trueCount); //1

You can use the forEach() method to count truthy values as well. For that, you can pass a function that returns 1 for all truthy values and 0 for falsy values. Thus, the counter variable gets incremented for every truthy value in the array.

For example –

var arr = [“a”, “b”, true, false, 1, 0];

var truthyCount = 0;

arr.forEach(value => {

  truthyCount += Boolean(value);

}); //4

console.log(truthyCount); //4

As you can see from the output, this code returns 4 – which is the total number of truthy values in the array.

Thus, these are some of the ways you can count the true values in an array in JavaScript.

I hope this helps!

Leave a Comment