Checking whether an object is empty is a common and repetitive task for JavaScript and TypeScript developers.
This is a crucial step, especially when dealing with data obtained from a database, user input, or APIs. Ensuring the existence of data before performing further operations is a fundamental practice.
In this article, we will explore various methods to check for empty objects in JavaScript, which also applies to TypeScript since it is a superset of JavaScript.
Check for an Empty Object Using the Object.keys() Method
One of the most straightforward ways to check if an object is empty in JavaScript is by using the Object. keys() method.
This method returns an array containing the keys of the object passed to it. By checking the length of this array, we can determine if the object is empty.
const data = {
name: "Subodh",
age: 23,
};
const message =
typeof data === "object" && Object.keys(data).length === 0
? "object is empty"
: "object is not empty";
console.log(message); // "object is not empty"
In this example, since the object data contains values, the output will indicate that the object is not empty. You can also achieve the same result using an if-else statement:
if (typeof data === 'object' && Object.keys(data).length === 0){
console.log('object is empty');
} else {
console.log('object is not empty'); // This gets printed
}
Here is another interesting article: How much JavaScript is required to start a career in software development?
Other Methods
Aside from the Object.keys() method, several other methods exist to check whether an object is empty in JavaScript.
- Object. values() Method: This method extracts the values from an object and returns them as an array. You can then check the length of this array to determine if the object is empty.
console.log(Object.values(data)); // [ 'Subodh', 23 ]
- Object.entries() Method: Similar to Object.keys(), this method returns an array, but it contains key-value pairs of the object. Checking the length of this array can also help determine if the object is empty.
console.log(Object.entries(data)); // [ [ 'name', 'Subodh' ], [ 'age', 23 ] ]
- hasOwnProperty() Method: If your browser does not support the keys(), values(), or entries() methods, you can use a for…in a statement in combination with hasOwnProperty() to iterate over the object’s properties and check if any property exists.
javascript
function isEmpty(obj) {
for (const prop in obj) {
if (obj.hasOwnProperty(prop)) {
return false;
}
}
return true;
}
console.log(
isEmpty({
name: "Subodh",
age: 23,
})
); // false
console.log(isEmpty({})); // true
- JSON.stringify() Method: Another method is to convert an object to a JSON string and check if the resulting string is empty. The JSON.stringify() method converts a JavaScript object into a JSON string.
console.log(JSON.stringify({
name: "Subodh",
age: 23
}));
// {"name":"Subodh","age":23}
console.log(JSON.stringify({}));
// {}
You can determine if the object is empty by comparing the JSON string to an empty JSON string (‘{}’).
function isEmpty(obj) {
return JSON.stringify(obj) === "{}";
}
console.log(
isEmpty({
name: "Subodh",
age: 23,
})
); // false
console.log(isEmpty({})); // true
Here is another interesting article on How to Define Custom Enums in JavaScript.
Conclusion
This article explored various methods to determine if an object is empty in JavaScript and TypeScript.
These methods include using the static Object methods entries(), keys(), and values() along with the length property, the hasOwnProperty() method in a loop, and the JSON.stringify() method.
By choosing the best method for your needs and browser compatibility, you can reliably check for empty objects in your JavaScript and TypeScript projects.
People also love to read another interesting article about Camel Case and Snake Case
Leave a Reply