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 17, 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 the length of that array.
<!-- 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 `Object.keys()` method returns an array of a given object's property names.
- Checking the length of this array allows you to determine if the object has any properties.
- An object with no properties will have a length of 0, indicating it is empty.
- This method only checks for own properties, not inherited ones.
Recommended Links:
