JavaScript: Check if all Values in Array are True or False

JavaScript: Check if all Values in Array are True or False

Introduction

There are cases in which you might need to verify that many different conditions are true, or a variable number of conditions. You can't usually use a simple if statement for these kind of cases, which means you need to store the boolean conditions in an array. But then how do we check if all of those conditions are true or false?

In this Byte, we'll explore a few different ways to check for all true, false, null, truthy, and falsey values in an array.

Checking for All True Values in an Array

Suppose we have an array of Boolean values and we want to check if all values in the array are true. We can use the Array.prototype.every() function, which tests whether all elements in the array pass the test implemented by the provided function.

Here's how you can do it:

let booleanArray = [true, true, true, true];

let allTrue = booleanArray.every(val => val === true);

console.log(allTrue); // outputs: true

In this code, Array.prototype.every() checks each value in the booleanArray to see if it's equal to true. If all values are true, it returns true; otherwise, it returns false.

Verifying All False Values in an Array

Unfortunately you can't just take the inverse value (i.e. !booleanArray.every()) from above, as this problem isn't that simple. I hate to admit, but this was the naive approach I tried before actually thinking through the problem...

Anyway, luckily we can still use the Array.prototype.every() function. This time, we'll just check if each value is equal to false.

Here's the code:

let booleanArray = [false, false, false, false];

let allFalse = booleanArray.every(val => val === false);

console.log(allFalse); // outputs: true

We could just use booleanArray.every(val => !val), but that would also include null or undefined values. So if the value must be false, you must be more explicit in your code.

So in the above code, if all values are false, it returns true; otherwise, it returns false.

Get free courses, guided projects, and more

No spam ever. Unsubscribe anytime. Read our Privacy Policy.

Note: One thing to note that Array.prototype.every() stops checking as soon as it finds a value that does not pass the test. In other words, it won't check the entire array if it doesn't need to.

Inspecting for Null Values in an Array

Sometimes, we need to check if an array contains any null values. For this, we can use the Array.prototype.includes() function, which determines whether an array includes a certain value among its entries.

Here's how to do it:

let mixedArray = [true, null, false, true];

let containsNull = mixedArray.includes(null);

console.log(containsNull); // outputs: true

In this example, Array.prototype.includes() checks each value in the mixedArray to see if it's equal to null. If it finds a null value, it returns true; otherwise, it returns false.

Confirming All Truthy Values in an Array

In JavaScript, a "truthy" value is a value that is considered true when encountered in a Boolean context. All values are truthy unless they are defined as falsy (i.e., except for false, 0, -0, 0n, "", null, undefined, and NaN).

If you want to confirm that all values in an array are truthy, you can use the every() method. This method tests whether all elements in the array pass the test implemented by the provided function. Here's how you can do it:

let arr = [1, 'hello', {}, [], 'JavaScript', true];

let allTruthy = arr.every(Boolean);

console.log(allTruthy); // true

In this example, the every() method checks each element in the array to see if it's truthy. If all elements are truthy, it returns true; otherwise, it returns false.

Validating All Falsy Values in an Array

On the other hand, a "falsy" value is a value that is considered false when encountered in a Boolean context. JavaScript recognizes false, 0, -0, 0n, "", null, undefined, and NaN as falsy.

To verify that all values in an array are falsy, you can again use the every() method, but this time with a function that checks for falsy values:

let arr = [0, '', null, undefined, NaN, false];

let allFalsy = arr.every((val) => !Boolean(val));

console.log(allFalsy); // true

In this code, the every() function checks each element in the array to see if it's falsy. If all elements are falsy, it returns true; otherwise, it returns false.

Conclusion

Checking for truthy or falsy values in an array is a common task, and JavaScript provides the every() method to make this task easier. By using this method with an appropriate test function, you can confirm whether all elements in an array are truthy or falsy.

Last Updated: August 16th, 2023
Was this helpful?
Project

React State Management with Redux and Redux-Toolkit

# javascript# React

Coordinating state and keeping components in sync can be tricky. If components rely on the same data but do not communicate with each other when...

David Landup
Uchechukwu Azubuko
Details

© 2013-2024 Stack Abuse. All rights reserved.

AboutDisclosurePrivacyTerms