Check if all Values in Array are True/False in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 4, 2024
4 min

banner

# Table of Contents

  1. Check if all Values in an Array are True in JavaScript
  2. Check if all Values in an Array are False in JavaScript

# Check if all Values in an Array are True in JavaScript

To check if all values in an array are true:

  1. Use the Array.every() method to iterate over the array.
  2. Compare each value to true and return the result.
  3. The every() method will return true if all values in the array are true.
index.js
function allAreTrue(arr) { return arr.every(element => element === true); } console.log(allAreTrue([true, true])); // ๐Ÿ‘‰๏ธ true console.log(allAreTrue([true, false])); // ๐Ÿ‘‰๏ธ false if (allAreTrue([true, true])) { // ๐Ÿ‘‡๏ธ this runs console.log('All values are true'); } else { console.log('NOT all values are true'); }

check if all values in array are true

The code for this article is available on GitHub

The same approach can be used to check if all values in an array pass a test.

The Array.every() method checks if all elements in the array pass the test implemented by the callback function.

On each iteration, we check if the current element is equal to true and return the result.

The method returns true if all elements pass the test and false otherwise.

If the function returns a falsy value, the every() method short-circuits and returns false.

If the condition is met for all array elements, the every method returns true.

Note that there is a distinction between a value being equal to true and a value being truthy.

The falsy values in JavaScript are: false, null, undefined, 0, "" (empty string), NaN (not a number).

All other values are truthy.

# Checking if all values in an array are null

If you need to check if all array values are null, compare each array element to null.

index.js
function allAreNull(arr) { return arr.every(element => element === null); } console.log(allAreNull([null, null])); // ๐Ÿ‘‰๏ธ true console.log(allAreNull([null, undefined])); // ๐Ÿ‘‰๏ธ false if (allAreNull([null, null, null])) { console.log('All values are equal to null'); } else { console.log('Not all values are equal to null'); }

check if all values in array are null

The code for this article is available on GitHub

The function takes an array as a parameter and returns true if all values values in the array are equal to null.

# Check if all values in an array are Truthy in JavaScript

To check if all values in an array are truthy:

  1. Use the Array.every() method to iterate over the array.
  2. On each iteration, return the current element directly.
  3. The every() method will return true if all array elements are truthy.
index.js
function allAreTruthy(arr) { return arr.every(element => element); } console.log(allAreTruthy([1, 'test', true])); // ๐Ÿ‘‰๏ธ true console.log(allAreTruthy([0, '', true])); // ๐Ÿ‘‰๏ธ false

check if all values in array are truthy

The code for this article is available on GitHub

The every() method checks if the passed-in callback function returns a truthy value, so we can directly return the current value.

An alternative and a little more concise approach is to use the Boolean() constructor.

index.js
function allAreTruthy(arr) { return arr.every(Boolean); } console.log(allAreTruthy([1, 'test', true])); // ๐Ÿ‘‰๏ธ true console.log(allAreTruthy([0, '', true])); // ๐Ÿ‘‰๏ธ false

The Boolean() constructor gets passed each value in the array, converts the value to its boolean representation and returns the result.

This achieves the same result but is a bit more implicit.

# Check if all Values in an Array are False in JavaScript

To check if all values in an array are false:

  1. Use the Array.every() method to iterate over the array.
  2. Compare each value to false and return the result.
  3. The every() method will return true if all array elements are equal to false.
index.js
function allAreFalse(arr) { return arr.every(element => element === false); } console.log(allAreFalse([false, false])); // ๐Ÿ‘‰๏ธ true console.log(allAreFalse([false, true])); // ๐Ÿ‘‰๏ธ false
The code for this article is available on GitHub

The function we passed to the Array.every method gets called with each element of the array.

On each iteration, we check if the current element is equal to false and return the result.

If all invocations of the callback function return a truthy value, then the Array.every() method returns true, otherwise, false is returned.

If the callback function we passed to the Array.every() method returns a falsy value, then Array.every() short-circuits also returning false.

The falsy values in JavaScript are: false, null, undefined, 0, "" (empty string), NaN (not a number). All other values are truthy.

Note that checking if a value is equal to false is different than checking if the value is falsy.

# Check if all Values in an Array are Falsy in JavaScript

To check if all values in an array are falsy:

  1. Use the Array.every() method to iterate over the array.
  2. Negate each value using the logical NOT (!) operator and return the result.
  3. The every() method will return true if all values in the array are falsy.
index.js
function allAreFalsy(arr) { return arr.every(element => !element); } console.log(allAreFalsy([0, '', false])); // true console.log(allAreFalsy([1, 'test', true])); // false
The code for this article is available on GitHub

On each iteration, we used the logical NOT (!) operator to convert each value to a boolean and invert the result.

Here are some examples of using the logical NOT (!) operator.

index.js
console.log(!true); // ๐Ÿ‘‰๏ธ false console.log(!false); // ๐Ÿ‘‰๏ธ true console.log(!'str'); // ๐Ÿ‘‰๏ธ false console.log(!''); // ๐Ÿ‘‰๏ธ true console.log(!null); // ๐Ÿ‘‰๏ธ true

If the array contains only falsy values, the function we passed to the every() method would return true on all iterations.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev