JS check object empty: In this article, we are going to check whether the object is empty or not in Javascript
Check whether an Object is Empty in JavaScript?
Javascript check object empty: We can check whether the object is empty or NOT in a number of ways. Let us see them one by one:
- Using EmailFilter
- Iterating Over the Properties of Object
- Using Object.entries() Method (ECMA 7+)
- Using hasOwnProperty() function (Pre-ECMA 5)
Method #1: Using Object.keys() Method
In JavaScript, use the following steps to determine whether an object is empty:
- Pass the object to the ‘Object.keys’ method to obtain an array of all the keys of an object
- Access the length property on the array.
- If the length of the keys is equal to 0, the object is empty.
Using ‘Object.keys’ on an Empty Object
// This Supports in IE 9-11 // Declare an empty object const object = {}; // Check if the above object is empty using the Object.keys and length attribute const chk_isEmpty = Object.keys(object).length === 0; // If it is empty it prints true, else false console.log(chk_isEmpty)
Output:
true
Explanation:
Here, the Object.keys() method is used to get an array of all of the keys of an object.
Using ‘Object.keys’ on a Non-Empty Object
// This Supports in IE 9-11 // Declare an empty object const object = {1:"Hello", 2:"this is", 3:"Btechgeeks"}; // Check if the above object is empty using the Object.keys and length attribute const chk_isEmpty = Object.keys(object).length === 0; // If it is empty it prints true, else false console.log(chk_isEmpty)
Output:
false
Explanation:
Here the given object is not empty, hence it returns false
NOTE:
If the object has no key-value pairs (if it is empty), the Object.keys method returns an empty array.
Method #2: Iterating Over the Properties of Object
Another way is to iterate over the properties of an object. If the object has even a single iteration, it is not empty.
Example
// This supports in IE 6-11 // Declare an empty object and store it in a variable const gvn_obj = {}; // Create a function say checkIsEmpty by passing the object // as an argument to it. function checkIsEmpty(object) { // Iterate over the properties of an object using the for loop. // Check If the object has even a single iteration(object is not empty) for (const property in object) { // As the object is NOT empty, return false return false; } // As the object is empty, return true return true; } // Pass the given object as an argument to the above checkIsEmpty // function and print the result console.log(checkIsEmpty(gvn_obj));
Output:
true
Method #3: Using Object.entries() Method (ECMA 7+)
// Declare an empty object and store it in a variable const gvn_obj = {}; // Check if the above object is empty using the Object.entries and length attribute const chk_isEmpty = Object.entries(gvn_obj).length === 0 && gvn_obj.constructor === Object //If it is empty it prints true, else false console.log(chk_isEmpty)
Output:
true
Method #4: Using hasOwnProperty() function (Pre-ECMA 5)
Example1
// Declare an empty object and store it in a variable const gvn_obj = {}; // Create a function say checkIsEmpty by passing the object // as an argument to it. function checkIsEmpty(gvn_obj) { for(var property in gvn_obj) { // using hasOwnProperty() function to check if an object is empty or NOT if(gvn_obj.hasOwnProperty(property)) { return false; } } return JSON.stringify(gvn_obj) === JSON.stringify({}); } // Pass the given object as an argument to the above checkIsEmpty // function and print the result console.log(checkIsEmpty(gvn_obj));
Output:
true
Example2
// Declare an object and store it in a variable const gvn_obj = {1:"Hello", 2:"this is", 3:"Btechgeeks"}; // Create a function say checkIsEmpty by passing the object // as an argument to it. function checkIsEmpty(gvn_obj) { for(var property in gvn_obj) { // using hasOwnProperty() function to check if an object is empty or NOT if(gvn_obj.hasOwnProperty(property)) { return false; } } return JSON.stringify(gvn_obj) === JSON.stringify({}); } // Pass the given object as an argument to the above checkIsEmpty // function and print the result console.log(checkIsEmpty(gvn_obj));
Output:
false