How to Check if an Object is Empty in TypeScript ?
Last Updated :
12 Mar, 2024
In TypeScript, it's common to encounter scenarios where you need to determine if an object is empty or not. An empty object typically means it contains no properties or all its properties are either undefined or null.
Below are the methods to check if an object is empty or not in TypeScript:
This approach involves using the Object.keys() method to extract all the keys of the object and then checking the length of the resulting array.
Example: In this example, we create an empty object `myObject` and then use the `Object.keys()` method to check if it is empty.
JavaScript
const obj1: Record<string, any> = {};
const obj2: Record<string, any> =
{ name: "John", age: 30 };
if (Object.keys(obj1).length === 0) {
console.log('obj1 is empty');
} else {
console.log('obj1 is not empty');
}
if (Object.keys(obj2).length === 0) {
console.log('obj2 is empty');
} else {
console.log('obj2 is not empty');
}
Output:
obj1 is empty
obj2 is not empty
This approach utilizes the Object.entries() method to get an array of key-value pairs from the object and then checks the length of the array.
Example: This example shows the use of the above-explained approach.
JavaScript
const obj3: Record<string, any> = {};
const obj4: Record<string, any> =
{ name: "Alice", city: "New York" };
if (Object.entries(obj3).length === 0) {
console.log('obj3 is empty');
} else {
console.log('obj3 is not empty');
}
if (Object.entries(obj4).length === 0) {
console.log('obj4 is empty');
} else {
console.log('obj4 is not empty');
}
Output:
obj3 is empty
obj4 is not empty
By iterating through all enumerable properties of the object using a for...in loop, this approach checks if the object has any enumerable properties.
Example: This example shows the use of the above-explained approach.
JavaScript
const obj5: Record<string, any> = {};
const obj6: Record<string, any> =
{ fruit: "Apple", quantity: 5 };
function isEmpty(obj: Record<string, any>): boolean {
for (const key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
return false;
}
}
return true;
}
if (isEmpty(obj5)) {
console.log('obj5 is empty');
} else {
console.log('obj5 is not empty');
}
if (isEmpty(obj6)) {
console.log('obj6 is empty');
} else {
console.log('obj6 is not empty');
}
Output:
obj5 is empty
obj6 is not empty
Converting the object into a JSON string allows for easy checking if it's empty by verifying the length of the resulting string.
Example: This example shows the use of the above-explained approach.
JavaScript
const obj7: Record<string, any> = {};
const obj8: Record<string, any> =
{ language: "TypeScript", version: "4.5" };
if (JSON.stringify(obj7) === '{}') {
console.log('obj7 is empty');
} else {
console.log('obj7 is not empty');
}
if (JSON.stringify(obj8) === '{}') {
console.log('obj8 is empty');
} else {
console.log('obj8 is not empty');
}
Output:
obj7 is empty
obj8 is not empty
Similar Reads
Check if an Object is Empty in TypeScript In TypeScript, determining whether an object is empty involves checking if the object has any properties. This can be achieved through various built-in methods and loops. In this article, we will explore three different approaches to check if an object is empty in TypeScript.Table of ContentUsing Ob
2 min read
Check if an Array is Empty or not in TypeScript In TypeScript, while performing any operation on the array, it is quite important that there should be elements or data in the array, else no operation can be done. We can check whether the array contains the elements or not by using the below-listed methods: Table of Content Using length PropertyUs
2 min read
How to Check if an Array Includes an Object in TypeScript ? In TypeScript, checking if an array includes an object consists of comparing the object's properties within the array elements. We can compare and check this using three different approaches some method, find method, and includes method. There are several ways to check if an array includes an object
3 min read
How to Check if all Enum Values Exist in an Object in TypeScript ? To check if all values of an enum exist in an object in TypeScript, iterate through the enum values and verify their presence in the object. This ensures that the object encompasses all possible enum values, confirming completeness and adherence to the enum definition. There are various methods to c
3 min read
How to Return an Empty Promise in TypeScript ? In TypeScript, you can return an empty promise to create an empty asynchronous task. There are mainly two ways that can be used to create empty promises in TypeScript as listed below. Table of Content By using the Promise.resolve() methodBy immediately resolving the promiseUsing async/await to Retur
2 min read