Ask any question about JavaScript here... and get an instant response.
Post this Question & Answer:
How can I detect if an object is empty in JavaScript?
Asked on Feb 02, 2026
Answer
To determine if an object is empty in JavaScript, you can check if it has any enumerable properties using the Object.keys() method, which returns an array of the object's keys.
<!-- BEGIN COPY / PASTE -->
const isEmpty = (obj) => Object.keys(obj).length === 0;
const obj1 = {};
const obj2 = { key: 'value' };
console.log(isEmpty(obj1)); // true
console.log(isEmpty(obj2)); // false
<!-- END COPY / PASTE -->Additional Comment:
✅ Answered with JavaScript best practices.- The function "isEmpty" takes an object as an argument.
- "Object.keys(obj)" returns an array of the object's own enumerable property names.
- If the length of this array is zero, the object is considered empty.
- This method only checks for enumerable properties and does not consider non-enumerable properties or properties in the prototype chain.
Recommended Links:
