How To Count The True Values In An Array Using JavaScript

Count the True values in an Array using JavaScript

We will learn how to count the True values in an array using JavaScript two solutions: using filter() and forEach(). Let’s go into detail now.

Count the True values in an array using JavaScript

Using filter()

To count the True values in an array using JavaScript, you should sort out the true elements in an array and then print out the length (size) of the result. You can read its syntax and usage here.

Firstly, you should take a look at what the method return:

// Count the Truthy values in an Array
var array = [1, 0, true, "true", "Learn Share IT", 2022]
console.log(array.filter(Boolean))

Output: 

(5) [1, true, 'true', 'Learn Share IT', 2022]

The above example defined a new array to use the filter() function. We apply the Boolean function to filter out true values in an array. If you don’t want to use the Boolean function, but you want to make sure values are boolean and equal to true, you should use the === operator (strict equal) in the checking function, for example:

// Get the values that are equal to the "true" boolean in an Array
var array = [1, 0, true, "true", "Learn Share IT", 2022]
console.log(array.filter(v => v === true))

 Output: 

(1) [true]

As you can see, the array returned by the method is all filled with true values. Then we print out the size of it to count them:

// Count the values that are equal to the "true" boolean in an Array
var array = [1, 0, true, "true", "Learn Share IT", 2022]
console.log(array.filter(v => v === true).length)

Output: 

1

If testing truthy values rather than true booleans alone is what you want. Then, rather than using strict equals, you want to utilize an equal (== operator) or the Boolean function.

// Count the values that are true in an Array using ==
var array = [1, 0, true, "true", "Learn Share IT", 2022]
console.log(array.filter(v => v == true).length)

Output:

2
// Count the values that are true in an Array using Boolean
var array = [1, 0, true, "true", "Learn Share IT", 2022]
console.log(array.filter(Boolean).length)

Output: 

5

Have you noticed how the three methods of determining true values differ in the aforementioned examples? The method that works best for you will depend on what you truly desire. Use the boolean function to check for truthy values; the equal operator should be used to check for true strings but false integers and booleans. Additionally, use strict equal in the filter method to only check for true booleans.

Using forEach()

This method will execute a statement for each iteration:

Syntax:

forEach(function)

Parameter:

  • function: write the function directly that you want to do.

There are three distinct methods to apply this strategy depending on what genuine values you desire, as was clearly stated in the prior solution:

let count = 0
var array = [1, 0, true, "true", "Learn Share IT", 2022]

// Count the values that are equal to the "true" boolean in an Array
array.forEach(v => v === true?count++:count)
console.log(count)

Output:

1
let count = 0
var array = [1, 0, true, "true", "Learn Share IT", 2022]

// Count the values that are true in an Array using ==
array.forEach(v => v == true?count++:count)
console.log(count)

Output: 

2
let count = 0
var array = [1, 0, true, "true", "Learn Share IT", 2022]

// Count the values that are true in an Array using Boolean
array.forEach(v => Boolean(v)?count++:count)
console.log(count)

Output:

5

As can be seen, this method also produces the same results as the first solution. However, we suggest you read the first solution as it will help you to decide what kind of true values you are looking for.

Summary

We have discovered how to count the true values in an array using JavaScript. We suggest you use the second method; however you should read the first one before implementing it. We hope you succeed with our tutorials.

Maybe you are interested:

Leave a Reply

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