How to check if an object is empty or not in JavaScript :
In this tutorial, we will learn how to check if an object is empty or not in Javascript. This is one of the important things to learn if you are developing any javascript application. You can’t be sure that any object you are receiving either from an API or from a function will be non-empty always. So, instead of breaking your code, you should always check if an object is empty or not. This is a good coding practice and we should follow it on any programming language, not only in Javascript.
Method 1 : Object.getOwnPropertyNames :
Introduced in ECMAScript5
Object.getOwnPropertyNames method returns all properties found inside an object. We can use this method to get the list of properties and check if any property exists or not. If not, it is an empty object. For example :
var obj = {"1" : "Hello", "2" : "World"}
console.log(Object.getOwnPropertyNames(obj))
In this example, obj is not an empty object. If you run this program, it will print the below output :
[ '1', '2' ]
It is an array. For an empty object, it returns one empty array. So, we can check the size of the return array and based on that, we can determine if it is empty or not.
One exception is here. For new Date() object, it returns an empty array. So, to be on the safe side, we can add one extra check to determine if it is an object or not like below :
function isEmpty(o){
return (Object.getOwnPropertyNames(o).length === 0 && o.constructor === Object);
}
var obj = {"1" : "Hello", "2" : "World"}
var eObj = {}
console.log(`obj : ${isEmpty(obj)}`)
console.log(`eObj : ${isEmpty(eObj)}`)
It will print :
obj : false
eObj : true
So, you can create one function isEmpty and put it in a utility file to use it all over your application.
Method 2: Using Object.keys :
Similar to the above function, we have one more method called Object.keys. The return value of this method is an array of all keys in the object. So, we can check the length of the return array and say the object is empty or not. For example :
var obj = {1:"one", 2: "two", 3: "three"}
var eObj = {}
console.log(Object.keys(obj))
console.log(Object.keys(eObj))
This will print :
[ '1', '2', '3' ]
[]
We can create one separate function like the above example :
function isEmpty(o){
return (Object.keys(o).length === 0 && o.constructor === Object);
}
var obj = {1:"one", 2: "two", 3: "three"}
var eObj = {}
console.log(`obj : ${isEmpty(obj)}`)
console.log(`eObj : ${isEmpty(eObj)}`)
The output is :
obj : false
eObj : true
Method 3: Using Object.entries :
Object.entries method returns one array with all enumerable properties of the object. We can check the length of this array to verify if it is empty or not. Also, we should check that the constructor of the object variable is of type ‘Object’.
function isEmpty(o){
return (Object.entries(o).length === 0 && o.constructor === Object);
}
var obj = {1:"one", 2: "two", 3: "three"}
var eObj = {}
console.log(`obj : ${isEmpty(obj)}`)
console.log(`eObj : ${isEmpty(eObj)}`)
Output :
obj : false
eObj : true