Check if all Values in an Object are True in JavaScript

avatar

Borislav Hadzhiev

Last updated: Jul 25, 2022

banner

Photo from Unsplash

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

To check if all values in object are equal to true:

  1. Use the Object.values() method to get an array of the object's values.
  2. Call the every() method on the array.
  3. The every method will test if all values in the array are equal to true and will return the result.
index.js
// ✅ Check if equal to `true` const obj1 = { first: true, second: true, }; const areTrue = Object.values(obj1).every( value => value === true ); console.log(areTrue); // 👉️ true // ✅ Check if `truthy` const obj2 = { first: 'hello', second: true, third: 1, }; const areTruthy = Object.values(obj2).every( value => value ); console.log(areTruthy); // 👉️ true

We used the Object.values method to get an array of the object's values.

index.js
const obj1 = { first: true, second: true, }; // 👇️ [true, true] console.log(Object.values(obj1));

The next step is to call the Array.every method on the result.

The function we passed to the every method is a test function that gets called for each element in the array.

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.

In our first example, we explicitly check if the value is equal to true.

There is a distinction between the boolean value true and a truthy value.

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

If you return any other (than the aforementioned 6 values) for all iterations of the every method, the method returns true.

To check if an object only contains truthy values:

  1. Use Object.values() to get an array of the object's values.
  2. Call the every() method on the array.
  3. Return each value straight away.
index.js
const obj2 = { first: 'hello', second: true, third: 1, }; const areTruthy = Object.values(obj2).every(value => value); console.log(areTruthy); // 👉️ true

All of the values in the object are truthy, so the test function passes on all iterations because we return each value as is.

An alternative approach is to use a shorthand method, where we leverage the Boolean object.

To check if an object only contains truthy values:

  1. Use Object.values() method to get an array of the object's values.
  2. Pass the Boolean object to the every() method.
  3. The every method will return true if all object values are truthy.
index.js
// ✅ Check if `truthy` const obj2 = { first: 'hello', second: true, third: 1, }; const areTruthyShort = Object.values(obj2).every(Boolean); console.log(areTruthyShort); // 👉️ true

The Boolean object converts each element from the array of values to its boolean representation and returns the result.

This code snippet achieves the same result as the previous one, however is a little more concise and implicit.

Which approach you pick is a matter of personal preference. I'd go with the explicit approach of returning each value from the every method as I find it easier to read.

Further Reading #

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.