How to check if the object is empty using JavaScript

How to check if the object is empty using JavaScript

In this post we will give you information about How to check if the object is empty using JavaScript. Hear we will give you detail about How to check if the object is empty using JavaScriptAnd how to use it also give you demo for it if it is necessary.

In javascript, we can use the Object.getOwnPropertyNames() method to check if the given object is empty or not.

function isObjectEmpty(obj){   return Object.getOwnPropertyNames(obj).length >= 1}isObjectEmpty({ }) // falseisObjectEmpty({id:1,name:"js"}) // true

If we pass an object to the Object.getOwnPropertyNames() method it returns an array of object properties.so that by using the length of that array we can easily check if the given object is empty or not.

Similarly, we can use the Object.keys() or Object.values() method.

Example:

Object.values({ }).length >= 1  // falseObject.values({ id:1}).length >= 1  // trueObject.keys({id:1 }).length >=1 // true
  • The Object.keys() method returns an array with object keys.

  • The Object.values() method returns an array with object values.

Hope this code and post will helped you for implement How to check if the object is empty using JavaScript. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US