How to Check if an Object is Empty in JavaScript


Any object with no properties or only those inherited from its prototype chain will be considered to be an empty object. In JavaScript, it is common for developers to determine whether an object is empty or not. This can be done especially when working with data manipulation and validation.

This article will describe the methods for determining whether the JavaScript object is empty.

How to Check if an Object is Empty in JavaScript?

To check or verify whether an object is empty or not, use the following JavaScript pre-built methods:

  • Object.keys() Method
  • JSON.stringify() Method
  • For-in loop with hasOwnProperty() Method

Method 1: Check if an Object is Empty in JavaScript Using “Object.keys()” Method

Use the “Object.keys()” method for checking whether the object is empty or not. It gives an array of keys or enumerable property names of an object. We can verify whether the object is empty or not by determining the length of the returned array.

Syntax

The following syntax is utilized for determining the empty object:

Object.keys(object).length === 0

Example

First, define a function named “isEmpty()” that takes an object as a parameter and determines the length of the object keys. If the length is equivalent to “0”, it indicates an object is empty:

function isEmpty (object) { 
return Object.keys(object).length === 0; 
}

Now, declare two objects “obj1” and “obj2”:

var obj1 = {}; 
var obj2 = { 
name: 'Mari', 
age: 28 
};

Call the “isEmpty()” function by passing objects as an argument to check whether the given object is empty or not:

console.log("Object 1 is empty: " + isEmpty(obj1)); 
console.log("Object 2 is empty: " + isEmpty(obj2));

The output display “true” for the “obj1”, which indicates the object is empty whereas “false” for “obj2” which indicates that the object is not empty:

Method 2: Check if an Object is Empty in JavaScript Using “JSON.stringify()” Method

For determining if an object is empty, you can also use the “JSON.stringify()” method. It converts/change an object to a JSON string. For checking the empty object, compare the resulting string to an empty object string representation “{ }”.

Syntax

Use the given syntax for verifying an empty object with the help of the “JSON.stringify()” method:

JSON.stringify(object) === '{}'

Example

Define a function, where an object is converted into a JSON string and then compare it with an empty object string:

function isEmpty (object) {
return JSON.stringify(object) === '{}';
}

As you can see in the output, “obj1” is “true” which means the converted JSON string of an object is equivalent to the empty object string:

Method 3: Check if an Object is Empty in JavaScript Using “for-in” with “hasOwnProperty()” Method

Another way for verifying the object is empty is to iterate the object’s properties using a “for-in” loop and check whether the property exists with the help of the “hasOwnProperty()” method:

Example

Define a function that takes an object as a parameter. Iterate the object properties using the “for-in” loop and check the property by utilizing the “hasOwnProperty()” method. If the object doesn’t contain the property, it returns “false” else, it gives “true”:

function isEmpty (object) { 
for (let property in object) { 
if (object.hasOwnProperty(property)) { 
return false; 
} 
} 
return true; 
}

Output

We have compiled all the essential information related to verifying whether the object is empty or not.

Conclusion

For checking if an object is empty or not, utilize the JavaScript pre-built methods including “Object.keys()”, “JSON.stringify()”, or “for-in” loop with the “hasOwnProperty()” method. You can choose any of these JavaScript predefined methods for determining the empty object. This article described the methods to determine whether the object is empty in JavaScript.

Print Friendly, PDF & Email
Categories
Tags