How to check an object is empty using JavaScript? - GeeksforGeeks

30 Apr, 2019
Method 1: Using the Object.keys(object) method: The required object could be passed to the Object.keys(object) method which will return the keys in the object. The length property is used to the result to check the number of keys. If the length property returns 0 keys, it means that the object is empty.
Syntax:
Object.keys(object).length === 0
Example:
<!DOCTYPE html><html>      <head>    <title>        How to check an object is         empty using JavaScript?    </title></head>  <body>    <h1 style="color: green">        GeeksforGeeks    </h1>          <b>        How to check an object is        empty using JavaScript?    </b>          <p>        Click on the button to check        if the object is empty    </p>          <p>Output for empty object:         <span class="outputEmpty"></span>    </p>          <p>Output for non empty object:         <span class="outputNonEmpty"></span>    </p>      <button onclick="checkObject()">        Click here    </button>          <!-- Script to check the object is empty or not -->    <script type="text/javascript">        function checkObject() {                          let emptyObj = {}            let nonEmptyObj = {                title: 'Title 1',                info: 'Sample Info'            }              ans1 = (Object.keys(emptyObj).length === 0)            document.querySelector('.outputEmpty').textContent                    = ans1;              ans2 = (Object.keys(nonEmptyObj).length === 0)            document.querySelector('.outputNonEmpty').textContent                    = ans2;        }    </script></body>  </html>                    
Output:
Before clicking the button:
After clicking the button:
Method 2: Looping through the object using object.hasOwnProperty(key): A function is created where the object is looped over and checked if it contains the ‘key’ property using the object.hasOwnProperty() method. This function would return true if it could find no keys in the loop, which means that the object is empty. If any key is encountered, the loop breaks and false is returned. This method also works for older browsers that may not support the first method.
Syntax:
function isEmptyObj(object) {
    for (var key in object) {
        if (object.hasOwnProperty(key)) {
            return false;
        }
    }
}

Example:
<!DOCTYPE html><html>      <head>    <title>        How to check an object is        empty using JavaScript?    </title></head>  <body>    <h1 style="color: green">        GeeksforGeeks    </h1>          <b>        How to check an object is        empty using JavaScript?    </b>          <p>         on the button to check         if the object is empty    </p>          <p>        Output for empty object:         <span class="outputEmpty"></span>    </p>          <p>        Output for non empty object:         <span class="outputNonEmpty"></span>    </p>       <button onclick="checkObject()">        Click here    </button>          <script type="text/javascript">           function checkObject() {            let emptyObj = {}            let nonEmptyObj = {                title: 'Title 1',                info: 'Sample Info'            }               ans1 = isEmptyObj(emptyObj);            document.querySelector('.outputEmpty').textContent                    = ans1;               ans2 = isEmptyObj(nonEmptyObj);            document.querySelector('.outputNonEmpty').textContent                    = ans2;        }           function isEmptyObj(object) {            for (var key in object) {                if (object.hasOwnProperty(key)) {                    return false;                }            }               return true;        }    </script></body>  </html>
Output:
Before clicking the button:
After clicking the button:
jQuery-Misc
Picked
JavaScript
Web Technologies
Web technologies Questions
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Difference between var, let and const keywords in JavaScript
Difference Between PUT and PATCH Request
Angular File Upload
How to get selected value in dropdown list using JavaScript ?
How to remove duplicate elements from JavaScript Array ?
Express.js express.Router() Function
Installation of Node.js on Linux
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to fetch data from an API in ReactJS ?
Difference between var, let and const keywords in JavaScript