Explain Codes LogoExplain Codes Logo

How to check if object has any properties in JavaScript?

javascript
object-properties
javascript-utility-functions
property-enumeration
Nikita BarsukovbyNikita Barsukov·Feb 15, 2025
⚡TLDR

The quickest way to determine whether an object has any of its own properties is via the Object.keys(obj).length method. If it returns zero, the object is empty.

Example:

const isEmpty = Object.keys({}).length === 0; // true. As empty as a politician's promises... just kidding!

This method disregards inherited properties. It's a concise way to indicate whether an object owns any properties.

Deep-diving: alternative approaches

Handling non-enumerable properties

To account for non-enumerable properties, use Object.getOwnPropertyNames(obj).length. It's like Object.keys on steroids.

Example:

const hasNonEnumerable = Object.getOwnPropertyNames({prop: 'value'}).length > 0; // true. You can't hide from getOwnPropertyNames!

Checking specific properties

To confirm the existence of a specific property in an object without involving the prototype chain, put hasOwnProperty() to work.

Example:

const object = { ownProp: 'exists' }; const hasOwn = object.hasOwnProperty('ownProp'); // true. Property found? Achievement unlocked!

Counting user-defined properties only

If interested in user-defined properties alone, a for...in loop accompanied by hasOwnProperty() is your weapon of choice.

Example:

let userDefinedPropertyCount = 0; const object = { foo: 'bar', baz: 'qux' }; for (let key in object) { if (object.hasOwnProperty(key)) { userDefinedPropertyCount++; // Increments faster than my social media followers... } }

Leveraging libraries

For jQuery aficionados, $.isEmptyObject(obj) offers a consistently 'jQuery-esque' approach.

Example:

const isEmpty = $.isEmptyObject({}); // true. jQuery not just for DOM manipulation!

In an ES5 environment, consider shims to bolster older browser support for methods like Object.keys.

Nitty-gritty details

Grasping the prototype chain

To distinguish between inherited properties and user-defined properties, get to grips with the prototype chain.

Example:

const base = { inheritedProp: 'inherited' }; const obj = Object.create(base); obj.ownProp = 'own'; // 'inheritedProp' is inherited, 'ownProp' is user-defined. It's a family thing!

Controlling property visibility

You can decide which properties are enumerated by Object.defineProperty, controlling their enumerability.

Example:

const object = {}; Object.defineProperty(object, 'hidden', { value: 'secret', enumerable: false, }); // 'hidden' won't be counted by Object.keys(). More elusive than my diet plan...

Sidestepping issues

Keep an eye out for edge cases, like objects with inherited non-enumerable properties, which might sneak past your property count.

Streamlining patterns

Encapsulate enumeration logic in a utility function for repeated checks. Efficiency, thy name is isObjectEmpty()!

Example:

function isObjectEmpty(obj) { return Object.keys(obj).length === 0; } // Pretty handy function, huh?