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? Pending Review
Asked on Apr 19, 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 check its length.
<!-- BEGIN COPY / PASTE -->
const isEmpty = (obj) => Object.keys(obj).length === 0;
const myObject = {};
console.log(isEmpty(myObject)); // true
const anotherObject = { key: 'value' };
console.log(isEmpty(anotherObject)); // false
<!-- END COPY / PASTE -->Additional Comment:
✅ Answered with JavaScript best practices.- This method uses "Object.keys()" to retrieve all keys of the object.
- If the length of the keys array is 0, the object is considered empty.
- This approach works for plain objects but does not account for objects with non-enumerable properties or symbols.
- Always ensure that the input is an object to avoid unexpected results.
Recommended Links:
