JavaScript

Check if all Object Properties are Null in JavaScript

While handling the data in JavaScript, there is often a requirement to free up the consumed memory by removing unnecessary data. For instance, assigning the values to the null properties can assist in utilizing the present resources effectively. In such case scenarios, checking if all object properties are null in JavaScript reduces the overall compile time and improves memory management.

This blog explains how to check if all object properties are null using JavaScript.

How to Check if All Object Properties are Null Using JavaScript?

To check if all the object properties are null in JavaScript, invoke the “Object.values()” method in your program. The Object.values() method takes an object and returns its own enumerable property’s values in the form of an array. This method can be applied to accept an object as a parameter and check if each of its included properties holds a value “null”.

Syntax

Object.values(obj)

In the above syntax:

  • obj” refers to the object whose property values will be returned.

Example 1: Check If All Object Properties are Null
Let’s overview the below-stated example:

<script type="text/javascript">
let object = {x: null,y: null};
let objProp = Object.values(object).every(value => {
if (value === null) {
 return true;
}
return false;
});
console.log(objProp);
</script>

According to the above code snippet:

  • Create an object named “object” having the stated properties.
  • In the next step, apply the “Object.values()” method, taking the created object as an argument.
  • After that, the “every()” method will be applied to check for each object value through iteration.
  • If all the values are equal to “null”, a boolean value “true” will be displayed.
  • In the other case, “false” will be displayed on the console.

Output

From the above output, it is proved that all the object properties hold “null” values.

Example 2: Check If Object Properties are Null, Undefined, or False
This example will check for multiple values within the object and return the corresponding result:

<script type="text/javascript">
let object = {x: null, y: undefined, z: false};
let objProp = Object.values(object).every(value => {
if (value === null || value === undefined || value === false) {
 return true;
}
 return false;
});
console.log(objProp);
</script>

Perform the following steps as given in the above lines of code:

  • Likewise, create an object named “object” having the stated properties and assigned values.
  • After that, similarly, apply the “Object.values()” method such that the created object is checked for each of the specified values against a property in the stated condition with the help of the “OR(||)” operator.
  • In the case of a value being “null”, “undefined”, or “false”, a boolean value “true” will be returned.
  • In the other scenario, the value “false” will be displayed.

Output

In the above output, it can be observed that the added conditions are evaluated as true, so, the boolean value “true” is returned.

Conclusion

The “Object.values()” method can be implemented to check if all the object properties are null, undefined, or false in JavaScript. The discussed first example verifies if all the values of the object properties are null. Whereas the other example applies a condition to check for various values against the corresponding object properties. This tutorial explained to check whether all the properties in an object are null in JavaScript.

About the author

Umar Hassan

I am a Front-End Web Developer. Being a technical author, I try to learn new things and adapt with them every day. I am passionate to write about evolving software tools and technologies and make it understandable for the end-user.