Skip to main content

In Node.js, you can check if an object is empty by using the Object.keys() method to get an array of the object's keys, and then checking the length of the array.

Here's an example:

const obj = {};

if (Object.keys(obj).length === 0) {
  console.log('Object is empty');
} else {
  console.log('Object is not empty');

In the above code, Object.keys(obj) returns an empty array because the obj object has no keys. So Object.keys(obj).length will be 0, indicating that the object is empty.

If the object has properties, then Object.keys(obj) will return an array with the names of the object's properties, and Object.keys(obj).length will be greater than 0.