How to Check if a Dictionary is Empty in TypeScript?

Do you need to check if a dictionary is empty in Typescript? In this Typescript tutorial, I have explained how to check if a dictionary is empty in Typescript using various methods.

To check if a dictionary is empty in Typescript, you can use various methods like below:

  1. Using Object.keys()
  2. Using Object.getOwnPropertyNames()
  3. Using a for-in loop
  4. Using JSON.stringify()

Typescript check if a dictionary is empty using Object.keys()

The Object.keys() method is a convenient way to get all the keys of a dictionary as an array. If the array’s length is 0, it means the dictionary does not have any keys, implying that it is empty.

Here is a complete example to check if a dictionary is empty using Object.keys().

function isEmptyDict(dictionary: Record<string, any>): boolean {
  return Object.keys(dictionary).length === 0;
}

// Example usage:
const myDict: Record<string, any> = {};

if (isEmptyDict(myDict)) {
  console.log('The dictionary is empty.');
} else {
  console.log('The dictionary is not empty.');
}

You can see here I got the output “The dictionary is empty.” after executing the code using Visual Studio Code. Check out the screenshot below.

typescript check if dictionary is empty

Typescript check empty dictionary using Object.getOwnPropertyNames()

Here is another way to check an empty dictionary in Typescript using the Object.getOwnPropertyNames() method. Similar to Object.keys(), it returns an array of all properties found directly upon a given object.

Here is the complete code:

function isEmptyDict(dictionary: Record<string, any>): boolean {
  return Object.getOwnPropertyNames(dictionary).length === 0;
}

// Example usage:
const myDict: Record<string, any> = {};

if (isEmptyDict(myDict)) {
  console.log('The dictionary is empty.');
} else {
  console.log('The dictionary is not empty.');
}

Here is the output after I ran the code using Visual Studio code in the screenshot below:

typescript check empty dictionary

Check if a dictionary is empty in Typescript using a for-in loop

Now, let us check how to use a for-in loop to check if a dictionary is empty in Typescript. Using a for-in loop to iterate over the properties of the dictionary. If the loop executes, it means there is at least one property.

Here is the complete code:

function isEmptyDict(dictionary: Record<string, any>): boolean {
  for (const key in dictionary) {
    if (dictionary.hasOwnProperty(key)) {
      return false;
    }
  }
  return true;
}

// Example usage:
const myDict: Record<string, any> = {};

if (isEmptyDict(myDict)) {
  console.log('The dictionary is empty.');
} else {
  console.log('The dictionary is not empty.');
}

Here is the screenshot below after running the code using the VS code editor.

check if dictionary is empty typescript

Check if a dictionary is empty in Typescript using JSON.stringify()

Here is the final method to check if a dictionary is empty in Typescript using JSON.stringify(). Although not the most efficient for large objects, using JSON.stringify() can be a quick way to check for an empty object, as it converts the dictionary to its JSON string representation.

Here is the complete code:

function isEmptyDict(dictionary: Record<string, any>): boolean {
  return JSON.stringify(dictionary) === '{}';
}

// Example usage:
const myDict: Record<string, any> = {};

if (isEmptyDict(myDict)) {
  console.log('The dictionary is empty.');
} else {
  console.log('The dictionary is not empty.');
}

Conclusion

In this tutorial, I have explained different methods to check if a dictionary is empty in TypeScript. Each method has its own use case and performance considerations, whether you opt for Object.keys(), Object.getOwnPropertyNames(), the traditional for-in loop, or JSON.stringify(); your choice should depend on your specific context and performance needs in Typescript.

You may like the following tutorials:

>