How to check if an object is empty in JavaScript

Checking whether an object is empty is essential for form validation, API response handling, and conditional rendering in JavaScript applications. As the creator of CoreUI, a widely used open-source UI library, I’ve implemented empty object checks in countless production components over 25 years of development. From my expertise, the most reliable approach is using Object.keys() to check the length of enumerable properties. This method works consistently across all modern browsers and handles edge cases properly.

Use Object.keys() to check if an object has any enumerable properties.

const isEmpty = Object.keys(obj).length === 0

Here Object.keys(obj) returns an array of all enumerable property names from the object. If the array length equals 0, the object has no properties and is considered empty. This method correctly identifies {} as empty while properly handling objects with inherited properties or non-enumerable properties.

Best Practice Note:

This is the same approach we use in CoreUI components for form validation and conditional rendering. For objects that might be null or undefined, combine with a nullish check: obj && Object.keys(obj).length === 0 to avoid errors.


Speed up your responsive apps and websites with fully-featured, ready-to-use open-source admin panel templates—free to use and built for efficiency.


About the Author