Skip to content

Check if Object is empty in Javascript

Last updated on October 30, 2014

To check if an object is empty in JQuery we use jQuery.isEmptyObject( object ) with will return true on success , false on failure.

Ex :

jQuery.isEmptyObject({}); // true
jQuery.isEmptyObject({ foo: "bar" }); // false

To check if an object is empty or not using Javascript we use Object.getOwnPropertyNames() .The Object.getOwnPropertyNames() method returns an array of all properties (enumerable or not) found directly upon a given object.

var obj = {};
Object.getOwnPropertyNames(obj).length === 0; //true
 
obj = {foo : 'bar'};
Object.getOwnPropertyNames(obj).length === 0; //false

As a function :

function isEmptyObject(obj){
  return (Object.getOwnPropertyNames(obj).length === 0);
}

How to use :
isEmptyObject({}); // flase

That’s it.

0 0 votes
Article Rating
Subscribe
Notify of
guest

1 Comment
Most Voted
Newest Oldest
Inline Feedbacks
View all comments