How to check the boolean value of an array in javascript

This post talks about multiple ways of checking true/false values, that exist in an Array in Javascript/Typescript/Angular.

  • For loop to iterate each element, check if an element is a Boolean value using the if statement.
  • Array some() method, Performs iteration of elements and checks some of the elements found or not
  • Array indexOf method, This returns number (-1,0,1, etc) check index with
  • Es7 Array Includes that performs an element that exists in an array, returns true or false In this blog post, You’ll learn to check the A Boolean value of an array in javascript or typescript.

How to check Array contains a boolean value in javascript?

The array contains a collection of similar type values.

For example, we have a User form that contains multiple checkboxes - which can select All/Deselect All in the Angular application,

A checkbox in FormGroup is defined with FormArray which holds- if the checkbox is checked, false - checkbox is not checked.

To implement form validation like if a user is not checked any checkbox, display an error message - Please check at least one checkbox.

Then how do we check the boolean value true that exists in the array of checkboxes in Angular,

All the examples will work in Javascript, typescript, and Angular.

using For loop with if block example

A basic developer will use the below logic to check the true value in an array

  • Iterate the array used for the normal loop
  • Check each element for true value using the loop
  • if found, return true and break from the loop
  • if not found, return a false value
function checkTrueExistsArray(array) {
  for (var k = 0; k < array.length; k++) {
    if (array[k]) {
      return true;
      break;
    }
  }
  return false;
}
var arrayVariable = [false, false, true, false, true];
var arrayVariable1 = [false, false, false, false, false];

console.log(checkTrueExistsArray(arrayVariable)); //true
console.log(checkTrueExistsArray(arrayVariable1)); //false

using Array Some() method

Some() in an Array is an inbuilt method in javascript language.

  • It iterates each element and calls the callback for each value,
  • if the callback returns a true value, stop the iteration and return true
  • if the callback returns a false value, the iteration goes to the next iteration until it returns true or the array iteration is done.
function checkTrueUsingArraySome(array) {
  if (array.some((item) => item === true)) {
    return true;
  }
  return false;
}
var arrayVariable = [false, false, true, false, true];
var arrayVariable1 = [false, false, false, false, false];
console.log(checkTrueUsingArraySome(arrayVariable)); //true
console.log(checkTrueUsingArraySome(arrayVariable1)); //false

using Array indexOf method example

indexOf method in Array accepts input parameters, returns index position if input found in an array, else returns -1

function checkTrueUsingArrayIndexOf(array) {
  if (array.indexOf(true) != -1) {
    return true;
  }
  return false;
}
var arrayVariable = [false, false, true, false, true];
var arrayVariable1 = [false, false, false, false, false];

console.log(checkTrueUsingArrayIndexOf(arrayVariable)); //true
console.log(checkTrueUsingArrayIndexOf(arrayVariable1)); //false

using Es7 Includes checking the true value

ES7 i.e latest javascript language introduced the includes method

It returns true if the value is found in an array else returns false, You can check more about es7 includes

function checkTrueUsingArrayInclude(array) {
  if (array.includes(true)) {
    return true;
  }
  return false;
}
var arrayVariable = [false, false, true, false, true];
var arrayVariable1 = [false, false, false, false, false];

console.log(checkTrueUsingArrayInclude(arrayVariable)); //true
console.log(checkTrueUsingArrayInclude(arrayVariable1)); //false