How to check if an object is empty in JavaScript? In this article, we will cover a few examples to check if a JavaScript object is empty.

To check if an object is empty in JavaScript, you can use the Object.keys function to get an array of the object’s own enumerable properties and check if the length of the array is equal to 0.

Here is an example of how to use the Object.keys function to check if an object is empty:


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

const obj = {};
console.log(isEmpty(obj)); // Output: true

obj.name = 'John';
console.log(isEmpty(obj)); // Output: false

In this example, the isEmpty function takes an object as an argument and uses the Object.keys function to get an array of the object’s own enumerable properties.

If the length of the array is 0, the object is considered empty and the function returns true. Otherwise, the function returns false.

You can also use the Object.getOwnPropertyNames function to get an array of all own properties of an object, including non-enumerable properties.

However, this function returns an array of property names, rather than property values, so you will need to check the length of the array instead of checking for the presence of specific values.

Here is an example of how to use the Object.getOwnPropertyNames function to check if an object is empty:


function isEmpty(obj) {
return Object.getOwnPropertyNames(obj).length === 0;
}

const obj = {};
console.log(isEmpty(obj)); // Output: true

obj.name = 'John';
console.log(isEmpty(obj)); // Output: false

 

Using for…in loop to Check if Object is Empty


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

In the above code we uses a for...in loop to iterate through the properties of the object. It uses the hasOwnProperty() method to check if the current property belongs to the object and not to its prototype.

If it finds any property that belongs to the object, it returns false immediately, indicating that the object is not empty. If the loop completes without finding any properties that belong to the object, it returns true, indicating that the object is empty.

Using JSON.stringify to Check if an Object is Empty


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

uses JSON.stringify() method to convert the object to a JSON string.
Then it compares the JSON string of the object with the JSON string of an empty object {}.
If the strings are equal, it means the object is empty, it returns true.
If not, it returns false, indicating that the object is not empty.

There are a few other ways to check if an object is empty in JavaScript

Using the Object.entries() method


function isEmpty(obj) {
return !Object.entries(obj).length;
}

This method returns an array of key-value pairs for the object’s own enumerable properties and check if it’s length is zero.

Using the Object.getOwnPropertyNames() method


function isEmpty(obj) {
return !Object.getOwnPropertyNames(obj).length;
}

This method returns an array of all properties (enumerable or non-enumerable) of an object and check if it’s length is zero.

Using the Object.values() method:


function isEmpty(obj) {
return !Object.values(obj).length;
}

This method returns an array of all values of enumerable properties of an object and checks if it’s length is zero.

All of the above methods will check if the object is empty, but they may have slightly different behavior based on the properties of the object.

Keep in mind that these methods only work for objects. To check if other types of variables (e.g., arrays, strings, numbers) are empty, you will need to use different methods.

For example, to check if an array is empty, you can use the length property or the includes method. To check if a string is empty, you can use the length property or the trim method. To check if a number is equal to 0, you