Navigate back to the homepage

How to check empty object in typescript?

Infinitbility
TypeScript
November 9th, 2021 · 1 min read
How to check empty object in typescript?

Hello Friends 👋,

Welcome To Infinitbility! ❤️

This tutorial will help you to check objects are empty or not in typescript, here we are using the Object.keys() method to check object has value or not.

As we know, to check an empty array we are using array length to verify array is empty or not in an object there is .length property not available then we have to first create an object to array for length using Object.keys() method.

Let start today’s tutorial How to check the empty object in typescript?

The Object.keys() method creates a separate array of object keys and now we are able to check their length to verify object is empty or not.

Let create an empty object in typescript. here I’m creating a new object but you can also use the class object.

1let obj: object = {};

Now when we try to check their length obj.length, we will get undefined.

let create object keys to array and check the length.

1Object.keys(obj).length;

Well, when we get length property. let use in if and else to check object empty or not.

1let obj: object = {};
2
3console.log(obj.length); // undefined
4
5let objLength = Object.keys(obj).length; // 0
6
7if(objLength == 0){
8 console.log("Empty Object");
9}

Output

Empty, Object, typescript, Example

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo

Tutorials

Categories

More articles from Infinitbility

How to check a substring in a string in typescript?

How to check a substring in a string in typescript?

Example of check array is empty or not in typescript

November 8th, 2021 · 1 min read
How to check array is empty in typescript?

How to check array is empty in typescript?

Example of check array is empty or not in typescript

November 7th, 2021 · 1 min read
© 2020–2021 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]