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 May 10, 2026
Answer
To check if an object is empty in JavaScript, you can use the `Object.keys()` method, which returns an array of the object's own enumerable property names. If the length of this array is zero, the object is empty.
<!-- BEGIN COPY / PASTE -->
const obj = {};
const isEmpty = Object.keys(obj).length === 0;
console.log(isEmpty); // true
<!-- END COPY / PASTE -->Additional Comment:
✅ Answered with JavaScript best practices.- This method checks if the object has any own properties.
- `Object.keys()` only considers enumerable properties, which is usually what you want.
- This approach works for plain objects but not for objects with inherited properties.
- Always ensure that the object is not `null` or `undefined` before calling `Object.keys()` to avoid errors.
Recommended Links:
