Ask any question about JavaScript here... and get an instant response.
Post this Question & Answer:
How can I check if an object is empty in JavaScript?
Asked on Mar 10, 2026
Answer
To check if an object is empty in JavaScript, you can use the Object.keys() method to get an array of the object's keys and then verify if the array's length is zero.
<!-- BEGIN COPY / PASTE -->
const isEmptyObject = (obj) => Object.keys(obj).length === 0;
const obj = {};
console.log(isEmptyObject(obj)); // true
const nonEmptyObj = { key: 'value' };
console.log(isEmptyObject(nonEmptyObj)); // false
<!-- END COPY / PASTE -->Additional Comment:
✅ Answered with JavaScript best practices.- The function isEmptyObject takes an object as an argument and returns true if the object has no keys.
- Object.keys(obj) returns an array of the object's own enumerable property names.
- Checking the length of this array helps determine if the object is empty.
- This method only checks for own properties, not inherited ones.
Recommended Links:
