How to Check if Object is Empty in JavaScript

📋 Table Of Content
In this article, we will check how to check if an Object is empty in JavaScript.
Checking an empty Object is not as easy as finding an empty Array in JavaScript. In Array, we all need to find the length of the array using array.length
, if it's empty it will return 0.
However, with Object, we have different methods in JavaScript to check for empty Object. Let's check out each of them with examples.
To check if a JavaScript object is empty:
- Using
Object.keys()
method along with the length property to check for an empty object. - Check using
JSON.stringify()
method, if it returns an empty bracket ({ }
), it means the object is empty. - Using
Object.hasOwnproperty()
, to check for properties inside an object.
Method 1: Using Object.keys()
The Object.keys()
method in JavaScript returns an array of a given object's own enumerable property names as strings.
Syntax:
Object.keys(obj)
obj
: The object with its enumerable property name that will be returned.
Now, if the return array's length is 0, then we can say that the Object is empty. Let's check it with an example.
const object1 = {
//empty object
};
console.log(Object.keys(object1).length);
// expected output: 0
Now since we got 0 as a returned value, we know that the given object was empty.
You can also check empty Object using Object.values
 and Object.entries
.
Method 2: Using JSON.stringify
You also check if an object is empty using JSON.stringify
. All you need to do is stringify the object and if the return data is an empty bracket ({ }
), then we can say the object is empty.
Let's see an example.
const object1 = {
//empty object
};
console.log(JSON.stringify(object1))
// expected output: {}
We can write the above code more clearly so it returns a boolean for us to check.
const object1 = {
//empty object
};
console.log(JSON.stringify(object1) == '{}')
// expected output: true
IMPORTANT : Make sure you write the empty bracket
{ }
inside a string like this'{ }'
. If not, the returned value will be false.
Method 3: Using Object.hasOwnproperty()
If you are using ES3 or older, then you have to loop through each property using the Object.hasOwnProperty()
method.
The hasOwnProperty()
method in JavaScript returns a boolean which indicates if the specific property is present in the Object.
Syntax:
hasOwnProperty(prop)
Let's see with an example.
const objectStudent = {
number : 42
};
console.log(objectStudent.hasOwnProperty('number'));
// expected output: true
console.log(objectStudent.hasOwnProperty('name'));
// expected output: false
Here, we have an Object, objectStudent with a property number. And since the number property is present in the object it returns true
when we pass the property name to hasOwnProperty()
method.
Now using this method we can create a function that will loop through each property of an object. If the object is empty it will return true
, or else it will return false
.
function isEmpty(obj) {
for(var prop in obj) {
if(obj.hasOwnProperty(prop))
return false;
}
return true;
}
const testObj = { }
console.log(isEmpty(testObj)) // true
Here, since the given object does not contain any property it returns true
.
Method 4: Using Lodash and Underscore
Lodash and Underscore is an external library, and we can use it to check for an empty object. And it has supports for older browsers too.
Both have the same syntax _.isEmpty()
Example:
_.isEmpty({});
Method 5: Using jQuery
JQuery is also a JavaScript library that provide us with some handy methods with which we can check for empty object easily.
Example:
jQuery.isEmptyObject({});
Related Topics: