. Advertisement .
..3..
. Advertisement .
..4..
Object is the most used data type because of its extremely powerful flexibility in data handling. Surely, those who have learned about Javascript and Object data type will know methods like Object.keys(), Object.entries().. And in this article, we will introduce details and specifically the methods used to handle the “Check if an object is empty in JavaScript ” situation. Let’s find out together!
How To Check If An Object Is Empty In Javascript
Here are some of the simplest and easiest to understand approaches. Basically, to Check If An Object Is Empty In Javascript, users can choose one of six methods:
- Object.entries()
- Object.keys()
- JSON.stringify()
- Object.getOwnPropertyNames()
- ‘for…in’ loop
- jQuery.isEmptyObject()
Let’s go into each method in detail.
Method 1: Using Object.entries()
Object.entries in JavaScript is a method that takes all the properties and corresponding values in a JavaScript object and returns the result as an array.
Syntax:
Object.entries(obj);
By combining with the Length property, if the result is 0, it means that the object under test is an empty object. Let’s see the example below:
const person = {}
if (Object.entries(person).length === 0) {
// is empty
} else {
// is not empty
}
Method 2: Using Object.keys()
The Object.keys() method is similar to Object.entries(). It has the effect of taking all the property names in the javascript object and returning the result as an array.
Syntax:
Object.keys(obj);
Just like above, let’s see an example below to understand how the function works and how the result will be displayed.
Code Sample:
Object.keys({}).length === 0; // true
Object.keys({name: 'Atta'}).length === 0; // false
Method 3: Using JSON.stringify()
The JSON.stringify() method takes an object and returns a string representing the passed object.
Syntax:
console.log(JSON.stringify(object) === '{}');
To check if the object is empty or not, after applying the method, the return result will be {} so that we know it is empty. For example below:
function isEmptyObject(obj){
return JSON.stringify(obj) === '{}'
} / / true
Method 4: Using Object.getOwnPropertyNames()
This method requires you to have a deep understanding of the program, although it will take more time than the above methods, but it will still give the desired results. “Object.getOwnPropertyNames()” returns an array of the names of the properties present in the object.. By checking the length of the array, you can tell if the object is empty or not. Examples are as follows:
Object.getOwnPropertyNames({}).length === 0; // true
Object.getOwnPropertyNames({id: 1, name: 'John Doe'}).length === 0; // false
Method 5: Using ‘for…in’ loop
This is the method that is considered to give the fastest results. By using the for…in property loop. If no loop is returned, the above object is an empty object. For example :
function isEmpty(obj) {
for(var prop in obj) {
if(obj.hasOwnProperty(prop))
return false;
}
return true;
}
If there is a loop, the result will return false, if not, return true.
Method 6: Using jQuery.isEmptyObject()
The last method mentioned in this article is jQuery.isEmptyObject(). You can absolutely use it to check if an object is empty or not. The result returned if the object is empty will be true, and if not empty is false. For instance:
const obj = {};
obj.isEmpty(); // true
const site = {
title : 'Twitter'
};
site.isEmpty(); // false
Conclusion
ITtutoria hope you enjoyed our article about discovering the answer for your lesson “Ways to Check If An Object Is Empty In Javascript”. If you have any questions or concerns, please feel free to leave a comment. We are always excited when our posts can provide useful information!
Leave a comment