Check If All Values In Array Are true In JavaScript

To check if all values in array are true in JavaScript, you can use the Array.every() method. This method returns a Boolean value, indicating whether or not every element in the array meets the condition specified by the callback function.

check if all values are true in JavaScript

Array.every() To Check If All Values In Array Are True In JavaScript

To use this method, you pass in a callback function as an argument. This function should take in two parameters: the current element being processed, and its index in the array. The function should return a Boolean value.

If the callback function returns true for every element in the array, then Array.every() will also return true. Otherwise, it will return false.

Here’s an example of how to use Array.every() to check if all values in array are true in JavaScript:

function allValuesAreTrue (arr) {

  return arr.every(element => element === true);

}

console.log(allValuesAreTrue([true, true, true])); //true

console.log(allValuesAreTrue([true, false])); //false

In the above code, we have a function called allValuesAreTrue(). This function takes in an array as an argument and uses the Array.every() method to check if all values in the array are true.

Note that there is a difference between true and truthy values. In JavaScript, truthy values are values that evaluate to true when converted to a Boolean type, whereas true value is a Boolean type with a value of true.

For example, the following values are all truthy values:

  • “foo”
  • 1
  • true
  • -1
  • “0”
  • []
  • {}

However, only the value true is a true value. All other values are false values. This is important to keep in mind when using Array.every().

If you want to check if all values in an array are truthy (not just true), you can use the Array.every() method like this:

function allValuesAreTruthy (arr) {

  return arr.every(element => element);

}

console.log(allValuesAreTruthy([1, 2, 3])); //true

console.log(allValuesAreTruthy([0, false])); //false

Note that the callback function in the above code is just element => element. This is equivalent to saying if (element) { return true; } else { return false; }.

In other words, the function will return true if the element is truthy, and false otherwise.

The falsy values in JavaScript are – false, 0, “”, null, undefined, and NaN. Rest all values are truthy.

You can also use the Boolean() function to check if all values in an array are truthy:

function allValuesAreTruthy (arr) {

  return arr.every(element => Boolean(element));

}

console.log(allValuesAreTruthy([1, 2, 3])); //true

console.log(allValuesAreTruthy([0, false])); //false

The Boolean() function returns true if the value passed to it is truthy, and false otherwise.

Conclusion

In this article, you learned how to use the Array.every() method to check if all values in array are true in JavaScript. You also learned about truthy and falsy values in JavaScript.

If you have any questions, please leave a comment below.

Leave a Reply