Skip to main content

How to Check if All Values in an Array are Truthy or Falsy in JavaScript

A common task in data validation is to check if every element in an array meets a boolean condition. You might need to verify that all checkboxes are true, that all values are "truthy" (i.e., not empty or zero), or that all are "falsy." The standard, modern, and most effective tool for this is the Array.prototype.every() method.

This guide will teach you how to use the .every() method to solve this problem. You will learn the critical difference between checking for the specific boolean true and checking for a "truthy" value, and see how to apply this pattern to all boolean conditions.

The Core Method: Array.prototype.every()

The .every() method is designed for exactly this purpose. It tests whether every element in an array passes a test implemented by a provided function.

The logic:

  • It executes a "callback" function for each element in the array.
  • If the callback returns true for every single element, then .every() returns true.
  • If the callback returns false for even one element, .every() immediately stops and returns false. This makes it very efficient, as it "short-circuits" on the first failure.

CRITICAL: true vs. "Truthy"

This is the most important concept to understand for this task.

  • The Boolean true: A specific, primitive data type.
  • "Truthy" Values: A much broader category. A value is "truthy" if it coerces to true in a boolean context. Almost all values are truthy. The only exceptions are the six falsy values: false, 0, "", null, undefined, and NaN.
note

'hello', 1, and [] are all truthy, but none of them are === true. Your solution must account for this distinction.

Solution 1: Check if All Values are Truthy

This is the most common use case, often used to check if all fields in a collection have been filled out.

You can simply pass the Boolean constructor directly to the .every() method. This is a concise and highly readable shorthand.

const allTruthyArray = [1, 'hello', true, []];
const mixedArray = [1, 'hello', 0, true]; // Contains a falsy value (0)

function allAreTruthy(arr) {
return arr.every(Boolean);
}

console.log(allAreTruthy(allTruthyArray)); // Output: true
console.log(allAreTruthy(mixedArray)); // Output: false

How it Works:

  • .every(Boolean) is a shortcut for .every(element => Boolean(element)).
  • The Boolean() function explicitly converts each element to its boolean equivalent (true or false), which is exactly what .every() needs to see.

Solution 2: Check if All Values are Falsy

To check if every element in an array is one of the six falsy values, you can use the logical NOT (!) operator in the callback.

const allFalsyArray = [0, '', null, undefined, false, NaN];
const mixedArray = [0, '', 'hello', false]; // Contains a truthy value ('hello')

function allAreFalsy(arr) {
// `!element` will be `true` only if `element` is falsy.
return arr.every(element => !element);
}

console.log(allAreFalsy(allFalsyArray)); // Output: true
console.log(allAreFalsy(mixedArray)); // Output: false

Solution 3: Check if All Values are Exactly true (or false)

If you need to be more strict and check for the specific boolean value true (and not just any truthy value), you must use the strict equality operator (===) in your callback.

Check if All are true

const allTrueArray = [true, true, true];
const notAllTrueArray = [true, 1, 'true']; // Contains truthy values, but not `true`

function allAreTrue(arr) {
return arr.every(element => element === true);
}

console.log(allAreTrue(allTrueArray)); // Output: true
console.log(allAreTrue(notAllTrueArray)); // Output: false

Check if All are false

The logic is identical, just comparing to false.

function allAreFalse(arr) {
return arr.every(element => element === false);
}

console.log(allAreFalse([false, false, false])); // Output: true
console.log(allAreFalse([false, 0, ''])); // Output: false

Conclusion

The Array.prototype.every() method is the definitive tool for checking if all elements in an array meet a specific condition.

The key takeaways are:

  1. To check if all values are truthy, use the concise shorthand: array.every(Boolean).
  2. To check if all values are falsy, use the logical NOT operator: array.every(element => !element).
  3. To check if all values are strictly true or false, you must use an explicit comparison in the callback: array.every(element => element === true).
  4. Remembering the distinction between a boolean true and a "truthy" value is critical to using these methods correctly.