How to Check if Object is Empty in Node.js?

05-Oct-2022

.

Admin

How to Check if Object is Empty in Node.js?

Hello Friends,

Today, I would like to show you how to check if an object is empty in node.js. you can see check if the object is empty in nodejs. it's a simple example of check if an object is empty in node js. it's a simple example of node.js check if the object is empty. You just need to do some step to do nodejs check if the object is empty.

This example is check if object is empty in nodejs. i will give simply example of check if object is empty using isEmptyObj().

The first method is the Object.keys(object). The required object should be passed to the Object.keys(object) then it will return the keys in the object. The length property is used to check the number of keys. If it returns 0 keys, then the object is empty.

Let's start following example:

Example: 1


// create blank object

var obj = {};

// check object empty or not

function isEmpty(object) {

return Object.keys(object).length === 0;

}

// return true or false in object

var emptyObj = isEmpty(obj);

console.log(emptyObj);

Output:

true

Example: 2

// create object list

var obj = {name: 'Nicesnippest.com'};

// check object empty or not

function isEmptyObj(obj) {

for (let key in obj) {

if (obj.hasOwnProperty(key)) {

return false;

}

}

}

// return true or false in object

var emptyObj = isEmptyObj(obj);

console.log(emptyObj);

Output:

false

I hope it can help you...

#Node JS