How to check if an Object is empty or not

How to check if an Object is empty or not - Youvcode

by umangparekh

HTML

<h1>How to check if Array object is Empty or not</h1>

<h3>
  To check the output of below JS code please open console using<br/>
  <span style="color:blue">F12 or Ctrl+Shift+J</span>
</h3>

JavaScript

function isObjectEmpty(Obj) {
  for (var key in Obj) {
    if (Obj.hasOwnProperty(key))
      return false;
  }
  return true;
}

var myObj = {}; // Empty Object

console.log("myObj is empty ? :->", isObjectEmpty(myObj)); // returns "true"

var myObj = {
  "name": "youvcode"
}; // Empty Object

console.log("myObj is not empty :->", isObjectEmpty(myObj)); // returns "false"