{"id":333,"date":"2020-08-19T18:05:46","date_gmt":"2020-08-19T18:05:46","guid":{"rendered":"https:\/\/datatype.co.in\/blog\/?p=333"},"modified":"2020-08-26T09:50:07","modified_gmt":"2020-08-26T09:50:07","slug":"check-if-an-object-is-empty-in-javascript","status":"publish","type":"post","link":"https:\/\/datatype.co.in\/blog\/check-if-an-object-is-empty-in-javascript\/","title":{"rendered":"How to Check If An Object is Empty in JavaScript"},"content":{"rendered":"\n<p>There are various ways to check if an object is empty in JavaScript.<\/p>\n\n\n\n<p><strong>Method 1:<\/strong> Using JavaScript <code>JSON.stringify<\/code> method &#8211;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var obj = {};\nif(JSON.stringify(obj) === '{}') { \/\/Object is empty\n   \/\/Code here..\n}<\/code><\/pre>\n\n\n\n<p><strong>Method 2:<\/strong> Looping through object keys &#8211;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function isEmpty(obj) {\n  for(var i in obj) return false; \n  return true;\n}\nvar obj = {};\nisEmpty(obj) \/\/true<\/code><\/pre>\n\n\n\n<p><a href=\"https:\/\/datatype.co.in\/code-editor\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"Try yourself (opens in a new tab)\">Try yourself<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are various ways to check if an object is empty in JavaScript. Method 1: Using JavaScript JSON.stringify method &#8211; Method 2: Looping through object keys &#8211; Try yourself<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[149,100,148,146,147],"class_list":["post-333","post","type-post","status-publish","format-standard","hentry","category-javascript","tag-empty-object-check-in-javascript","tag-js-interview-question","tag-json-stringify","tag-object-empty-check-in-js","tag-test-if-an-object-is-empty-in-javascript","list-style-post"],"_links":{"self":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts\/333","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/comments?post=333"}],"version-history":[{"count":6,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts\/333\/revisions"}],"predecessor-version":[{"id":413,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts\/333\/revisions\/413"}],"wp:attachment":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/media?parent=333"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/categories?post=333"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/tags?post=333"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}