Open In App

How to Check if an Object is Empty in TypeScript ?

Last Updated : 12 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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:

Using Object.keys()

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

Using Object.entries()

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

Using a for…in loop

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

Using JSON.stringify()

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


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads