Loading...
JavaScript

How to Check If An Object is Empty in JavaScript

There are various ways to check if an object is empty in JavaScript.

Method 1: Using JavaScript JSON.stringify method –

var obj = {};
if(JSON.stringify(obj) === '{}') { //Object is empty
   //Code here..
}

Method 2: Looping through object keys –

function isEmpty(obj) {
  for(var i in obj) return false; 
  return true;
}
var obj = {};
isEmpty(obj) //true

Try yourself

Share this article with your friends
Leave a Reply

Your email address will not be published. Required fields are marked *