Skip to main content

How to Check if All Object Properties Meet a Condition in JavaScript

A common task in data validation is to check if every property in an object satisfies a certain rule. For example, you might need to verify that all fields in a form object are filled out (truthy), or that all settings are configured to a specific value (like null or true). The modern and most effective way to perform this check is by combining the Object.values() and Array.prototype.every() methods.

This guide will teach you this powerful pattern. You will learn how to use it to check if all of an object's values are null, truthy, falsy, or any other condition you define.

The Core Pattern: Object.values().every()

The logic for this task is a simple two-step process:

  1. Get all values: Use Object.values(myObject) to get an array containing only the values of the object's properties.
  2. Test every value: Use the .every() array method to test whether every element in the array passes a condition you provide.

This pattern is the foundation for all the solutions in this guide.

Solution 1: Check if All Property Values are null

Let's check if every property in an object has been explicitly set to null.

const myObject = {
propertyA: null,
propertyB: null,
propertyC: null,
};

// 1. Get an array of the object's values: [null, null, null]
const values = Object.values(myObject);

// 2. Check if every value in the array is strictly equal to null
const allAreNull = values.every(value => value === null);

console.log(allAreNull); // Output: true

If we change one of the values:

const mixedObject = {
propertyA: null,
propertyB: 'hello', // This is not null
propertyC: null,
};

const allAreNullMixed = Object.values(mixedObject).every(value => value === null);

console.log(allAreNullMixed); // Output: false

Solution 2: Check if All Property Values are Truthy

A "truthy" value is any value that is not one of the six "falsy" values (false, 0, "", null, undefined, NaN). This check is useful for validating that all fields in a form have been filled in.

const validForm = {
name: 'Alice',
email: 'alice@example.com',
age: 30,
};

// We can pass the `Boolean` constructor directly to .every() as a shorthand.
const allAreTruthy = Object.values(validForm).every(Boolean);

console.log(allAreTruthy);
// Output: true

const invalidForm = {
name: 'Bob',
email: '', // Empty string is falsy
age: 42,
};

const allAreTruthyInvalid = Object.values(invalidForm).every(Boolean);

console.log(allAreTruthyInvalid); // Output: false

Solution 3: Check if All Property Values are Falsy

Similarly, you can check if every property holds a falsy value.

const emptyConfig = {
user: null,
theme: '',
showTips: false,
};

const allAreFalsy = Object.values(emptyConfig).every(value => !value);

console.log(allAreFalsy); // Output: true

The condition !value will return true only for falsy values, which is exactly what .every() needs to see for all elements to pass the test.

How the .every() Method Works

The Array.prototype.every() method is a powerful tool for validation.

  • It executes a "callback" function for each element in an array.
  • If the callback function returns true for every single element, then .every() returns true.
  • If the callback function returns false for even one element, .every() immediately stops and returns false. This makes it very efficient, as it doesn't need to check the rest of the array once it finds a failure.

Conclusion

The Object.values().every() pattern is the definitive, modern, and most readable way to verify that all values in a JavaScript object meet a specific condition.

The key takeaways are:

  1. Use Object.values(obj) to get a simple array of all the object's own property values.
  2. Use the .every() method on that array to test each value against your condition.
  3. For a truthy check, you can use the concise shorthand: .every(Boolean).
  4. For a falsy check, use the callback: .every(value => !value).
  5. For specific value checks (like null), use an explicit comparison: .every(value => value === null).