Just like we use hashmaps in C++ that store data in the form of “key-value” pairs, We use Objects in Javascript. Objects are non-primitive data types that store data in the form of key-value pairs. This article will look into various methods to check whether a JavaScript object is empty or not.
5 Methods to Check If a JavaScript Object Is Empty
You may be required to check if a JavaScript object is empty or not when executing conditional logic. That way, if certain properties exist within an object, then we apply them. Also, when we process user input from external sources, we may need to validate that the data is not only present but also contains meaningful values. And don’t forget, about testing and debugging.
In this section, we will look into various methods to check if a JavaScript object is empty:
1) Using the Object.keys() Method
Keys are typically used as the names of the identifiers to access the object’s properties. The Object.keys() method allows us to obtain the keys of an object and store them in an array. By checking the length of this array, we can determine if the JavaScript object is empty. This method was introduced in ES6 and is supported in modern browsers.
Here’s an example:
//declaring two objects let studentObject1={ name:'John', rollNumber:12 }; let studentObject2={ //this is an empty object }; //declaring variables to store object keys let studentKeysArray1=Object.keys(studentObject1); let studentKeysArray2=Object.keys(studentObject2); //checking the length of the keys arrays if(studentKeysArray1.length==0){ console.log("studentObject1 is an empty object"); } else{ console.log("studentObject1 is not an empty object"); } if(studentKeysArray2.length==0){ console.log("studentObject2 is an empty object"); } else{ console.log("studentObject2 is not an empty object") }
Output:
studentObject1 is not an empty object
studentObject2 is an empty object
In this example, we have declared two objects. We have used the Object.keys() method to store the object keys in an array. Then we check the length of the the array. If the length of the array comes out to be zero, the object is empty otherwise it is not empty. Thus, we have obtained the desired result.
This method is also useful when we need to loop through a JavaScript object.
2) Using the Object.values() Method
Values represent the values corresponding to the keys in an object. The Object.values() method allows us to obtain the values corresponding to the keys of an object and store them in an array. By checking the length of this array, we can determine if the object is empty. This method was introduced in ES6 and is supported in modern browsers.
Here’s an example of the same:
//declaring two objects let studentObject1={ name:'John', rollNumber:12 }; let studentObject2={ //this is an empty object }; //declaring variables to store object values let studentValuesArray1=Object.values(studentObject1); let studentValuesArray2=Object.values(studentObject2); //checking the length of the Values arrays if(studentValuesArray1.length==0){ console.log("studentObject1 is an empty object"); } else{ console.log("studentObject1 is not an empty object"); } if(studentValuesArray2.length==0){ console.log("studentObject2 is an empty object"); } else{ console.log("studentObject2 is not an empty object") }
Output:
studentObject1 is not an empty object
studentObject2 is an empty object
In this example, we have declared two objects. We have used the Object.values() method to store the object values in an array. Then we check the length of the the array. If the length of the array comes out to be zero, the object is empty otherwise it is not empty. Thus, we have obtained the desired result.
3) Using the Object.entries() Method
The key-value pairs as a whole are called entries. The Object.entries() method is used to convert the key-value pairs of an object as entities to a 2D array. This method was also introduced in ES8 and is supported in modern browsers. If the array obtained comes out to be empty, i.e. the length of the 2d array is zero, then the object is empty.
Here’s an example of the same:
//declaring two objects let studentObject1={ name:'John', rollNumber:12 }; let studentObject2={ //this is an empty object }; //declaring variables to store object entries let studentEntriesArray1=Object.entries(studentObject1); let studentEntriesArray2=Object.entries(studentObject2); //checking the length of the Entries 2d arrays if(studentEntriesArray1.length==0){ console.log("studentObject1 is an empty object"); } else{ console.log("studentObject1 is not an empty object"); } if(studentEntriesArray2.length==0){ console.log("studentObject2 is an empty object"); } else{ console.log("studentObject2 is not an empty object") }
Output:
studentObject1 is not an empty object
studentObject2 is an empty object
In this example, we have declared two objects. We have used the Object.entries() method to store the object entries in a 2D array. Then we check the length of the the array. If the length of the array comes out to be zero, the object is empty otherwise it is not empty. Thus, we have obtained the desired result.
4) Using for…in loop
We can use the for…in loop to iterate over the keys or properties of the JavaScript object. We can use a for…in loop to iterate over the object’s properties to determine if it is empty.
Here’s an example of the same:
//function to check if an object is empty using for...in loop function checkEmptyObject(obj) { for(let property in obj){ if(obj.hasOwnProperty(property)) { return false; } } return true; } //declaring two objects let studentObject1={ name:'John', rollNumber:12 }; let studentObject2={ //this is an empty object }; //calling function and checking if object is empty if(checkEmptyObject(studentObject1)){ console.log("studentObject1 is an empty object"); } else{ console.log("studentObject1 is not an empty object"); } if(checkEmptyObject(studentObject2)){ console.log("studentObject2 is an empty object"); } else{ console.log("studentObject2 is not an empty object") }
Output:
studentObject1 is not an empty object
studentObject2 is an empty object
In this example, we use the for…in loop to traverse over the object’s properties or keys. We have made a function to check if the object is empty or not. If the function returns true, then the object is empty. Thus, we have obtained the desired result.
5) JSON.stringify()
The JSON.stringify() method converts a JavaScript object to a JSON string. By comparing the resulting string with an empty object string, “{ }”, we can easily check if the object is empty.
Let us see an example of the same:
//declaring two objects let studentObject1={ name:'John', rollNumber:12 }; let studentObject2={ //this is an empty object }; //using the JSON.stringify() method let studentObjectStr1=JSON.stringify(studentObject1); let studentObjectStr2=JSON.stringify(studentObject2); //checking if the objects are empty if(studentObjectStr1=="{}"){ console.log("studentObject1 is an empty object"); } else{ console.log("studentObject1 is not an empty object"); } if(studentObjectStr2=="{}"){ console.log("studentObject2 is an empty object"); } else{ console.log("studentObject2 is not an empty object") }
Output:
studentObject1 is not an empty object
studentObject2 is an empty object
In this example, we use the JSON.stringify method to convert the JavaScript object into a JSON string. If the converted JSON string is “{ }”, that is an empty JSON string, the object is empty. Thus, we have obtained the desired result.
Conclusion
In this article, we looked into various methods to check if an object is empty in JavaScript. We can use Object.keys() method, Object.values() method, Object.entries() method, for…in loop or the JSON.stringify() method to check whether an object is empty.