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:
Recommended Posts:
- How to check a JavaScript Object is a DOM Object ?
- How to check input file is empty or not using JavaScript/jQuery ?
- How to access an object having spaces in the object's key using JavaScript ?
- How to check empty/undefined/null string in JavaScript?
- Check if an array is empty or not in JavaScript
- What is the difference between Object.keys() and Object.entries() methods in JavaScript ?
- How to check whether an array is empty using PHP?
- How to check an HTML element is empty using jQuery ?
- How to empty an array in JavaScript?
- JavaScript | Remove empty elements from an array
- JavaScript TypeError - Reduce of empty array with no initial value
- Why to check both isset() and !empty() function in PHP ?
- How to check if an array includes an object in JavaScript?
- How to check a key exists in JavaScript object?
- How to check object is an array in JavaScript?
- JavaScript | Check if a key exists inside a JSON object
- How to check whether an object exists in javascript ?
- PHP | Type Casting and Conversion of an Object to an Object of other class
- HTML | DOM Object Object
- How to select first object in object in AngularJS?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


