Check if all Object Properties are Null in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 1, 2024
5 min

banner

# Table of Contents

  1. Check if all Object Properties are Null
  2. Check if all Object properties are True
  3. Check if all Object properties are Falsy
  4. Check if all values in an object are Truthy in JavaScript
  5. Set all Values in an Object to Null in JavaScript

# Check if all Object Properties are Null

To check if all of an object's properties have a value of null:

  1. Use the Object.values() method to get an array of the object's values.
  2. Use the Array.every() method to iterate over the array.
  3. Check if each value is equal to null.
  4. The every() method will return true if all values are null.
index.js
const obj = {a: null, b: null}; const isNullish = Object.values(obj).every(value => { if (value === null) { return true; } return false; }); console.log(isNullish); // ๐Ÿ‘‰๏ธ true

check if all object properties are null

The code for this article is available on GitHub

NOTE: If you just need to check if an object is empty, use the Object.keys() method.

index.js
const obj = {a: null, b: null}; if (Object.keys(obj).length === 0) { console.log('The object is empty'); } else { console.log('The object is NOT empty'); }

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

index.js
const obj = {a: null, b: null}; // ๐Ÿ‘‡๏ธ [null, null] console.log(Object.values(obj));

The function we passed to the Array.every() method gets called with each value in the array.

If the value is equal to null, the function returns true, otherwise false is returned.

The Array.every() method will return true only if the callback function returns true for all values in the array.

If the every() method returns true, then all of the object's values are null.

You can also check if the object's values are set to null, undefined or empty string, or any other value that your use case requires.

index.js
const obj = {a: null, b: undefined, c: ''}; const isNullUndefEmptyStr = Object.values(obj).every(value => { // ๐Ÿ‘‡๏ธ check for multiple conditions if (value === null || value === undefined || value === '') { return true; } return false; }); console.log(isNullUndefEmptyStr); // ๐Ÿ‘‰๏ธ true

The code sample checks if each value in the object is null, undefined or empty string.

We used the || (OR) operator, which means that only 1 of the 3 conditions has to be satisfied for the if block to run.

# Check if all Object properties are True

The same approach can be used if you need to check if all object properties are True.

index.js
const obj = { first: true, second: true, }; const allTrue = Object.values(obj).every( value => value === true ); console.log(allTrue); // ๐Ÿ‘‰๏ธ true

check if all object properties are true

The code for this article is available on GitHub

The allTrue variable stores a true value if all properties in the object are true and false otherwise.

# Check if all Object properties are Falsy

You can also check if all values in an object are falsy.

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

All other values in the language are truthy.

index.js
const obj = {a: null, b: undefined, c: '', d: 0, e: false}; const isFalsy = Object.values(obj).every(value => { if (!value) { return true; } return false; }); console.log(isFalsy); // ๐Ÿ‘‰๏ธ true

check if object properties are falsy

All of the values in the object are falsy, so the test function passes on all iterations.

We used the logical NOT (!) operator to convert each value to a boolean and flip the result.

Here are some examples of using the logical NOT operator.

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

The operator converts each value to its boolean representation and flips it.

If all of the values in the object are falsy, the test function we passed to the every() method would return true on all iterations.

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

To check if all values in an object are truthy:

  1. Use Object.values() to get an array of the object's values.
  2. Use the Array.every() method to iterate over the array.
  3. Return each value straight away.
index.js
const obj = { first: 'bobbyhadz.com', second: true, third: 1, }; const allTruthy = Object.values(obj).every(value => value); console.log(allTruthy); // ๐Ÿ‘‰๏ธ true

check if all values in object are truthy

The code for this article is available on GitHub

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

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

index.js
const obj = { first: 'bobbyhadz.com', second: true, third: 1, }; const areTruthyShort = Object.values(obj).every(Boolean); console.log(areTruthyShort); // ๐Ÿ‘‰๏ธ true

The Boolean() constructor converts each element in the array to its boolean representation and returns the result.

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

# Set all Values in an Object to Null in JavaScript

To set all values in an object to null:

  1. Use the Object.keys() method to get an array of the object's keys.
  2. Use the forEach() method to iterate over the array.
  3. Set each value in the object to null.
index.js
const obj = { name: 'Bobby', age: 30, country: 'Chile', }; Object.keys(obj).forEach(key => { obj[key] = null; }); // ๐Ÿ‘‡๏ธ { name: null, age: null, country: null } console.log(obj);

set all values in object to null

The code for this article is available on GitHub

We used the Object.keys() method to get an array of the object's keys.

index.js
const obj = { name: 'Bobby', age: 30, country: 'Chile', }; // ๐Ÿ‘‡๏ธ [ 'name', 'age', 'country' ] console.log(Object.keys(obj));

The next step is to use the Array.forEach() method to iterate over the array.

The function we passed to the forEach() method gets called with each element (key) in the array.

On each iteration, we set the value of the current key of null.

After the last iteration, all of the values in the object will be set to null.

An alternative approach is to not mutate the object, but create a new object using the Array.reduce() method.

# Set all Values in an Object to Null using reduce()

This is a three-step process:

  1. Use the Object.keys() method to get an array of the object's keys.
  2. Use the Array.reduce() method to iterate over the array.
  3. Set each object value to null and return the result.
index.js
const obj = { name: 'Bobby', age: 30, country: 'Chile', }; const newObj = Object.keys(obj).reduce((accumulator, key) => { return {...accumulator, [key]: null}; }, {}); // ๐Ÿ‘‡๏ธ { name: null, age: null, country: null } console.log(newObj);

set all values in object to null using reduce

The code for this article is available on GitHub

The function we passed to the Array.reduce() method gets called for each element in the keys array.

We set the initial value for the accumulator variable to an empty object.

On each iteration, we use the spread syntax (...) to unpack the key-value pairs of the accumulated object into a new object, setting the current property to null.

# Set all Values in an Object to Null using Object.fromEntries()

This is a three-step process:

  1. Use the Object.keys() method to get an array of the object's keys.
  2. Use the Array.map() method to get an array of key-value pairs.
  3. Use the Object.fromEntries() method to create an object with null values from the array.
index.js
const obj = { name: 'Bobby', age: 30, country: 'Chile', }; const newObj = Object.fromEntries(Object.keys(obj).map(key => [key, null])); // ๐Ÿ‘‡๏ธ { name: null, age: null, country: null } console.log(newObj);
The code for this article is available on GitHub

We called the Array.map() method on the array of keys.

The function we passed to the Array.map() method gets called with each element in the array.

On each iteration, we return an array containing the key and a null value.

index.js
const obj = { name: 'Bobby', age: 30, country: 'Chile', }; // ๐Ÿ‘‡๏ธ [ [ 'name', null ], [ 'age', null ], [ 'country', null ] ] console.log(Object.keys(obj).map(key => [key, null]));

The map() method returns a new array containing the values returned from the callback function.

The last step is to use the Object.fromEntries() method to create an object from the array of key-value pairs.

The Object.fromEntries() transforms a list of key-value pairs into an object.

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