Explain Codes LogoExplain Codes Logo

Is object empty?

javascript
object-introspection
browser-compatibility
javascript-objects
Anton ShumikhinbyAnton Shumikhin·Jan 4, 2025
TLDR

To determine if an object is empty, we can use the wonderful feature of Object.keys(), combined with our trusty friend .length:

const cat = {}; console.log(Object.keys(cat).length === 0); // true if empty - Cat's hiding? ¯\_(ツ)_/¯

Exploring the abyss of object emptiness

While pondering the emptiness of a JavaScript object may lead us into a philosophical rabbit hole, let's keep it simple. An object in JavaScript is regarded as empty if it does not possess any own, enumerable properties. However, it can still be empty even if it has non-enumerable properties, depending on which existential school of thought you are subscribing to today.

Object.keys(obj).length === 0 is a smooth and modern way of object introspection, yet beware! Older, grumpy browsers might not support it. Before jumping into the sparkling waters of Object.keys() or Object.getOwnPropertyNames(), do a double-check with the lifeguard – the browser compatibility data.

Embracing all property types

Should you be embracing inclusive programming and want to count non-enumerable properties as well, Object.getOwnPropertyNames(obj).length === 0 is your friend:

console.log(Object.getOwnPropertyNames(cat).length === 0); // true if empty // Cat has no secrets, enumerable or not!

jQuery – your browser handshake

If you are reaching out to a diverse audience with a plethora of browsers, jQuery sends a warm and friendly handshake:

console.log($.isEmptyObject(cat)); // true if empty - jQuery is cat-friendly too!

Library party: till efficiency do us part

The popular libraries like Lodash or Underscore offer _.isEmpty() function, your swiss army knife of emptiness.

console.log(_.isEmpty(cat)); // true if empty - Lodash: is the cat lost in lodash?

Whilst partying with Jazz of Lodash or dancing to the beats of Underscore, remember these add an abstraction layer which might affect performance.

Looping the loop

Looping around with for...in can sometimes yield faster results, mainly if we're dealing with large, non-empty objects:

function isObjectEmpty(object) { for (let key in object) { // Saw a key? Not your property, buddy! if (object.hasOwnProperty(key)) return false; // false alarm, it's not empty! } return true; // it's empty! Or is it ಠ_ಠ ? }

Dodging edge case bullets

Beware of the silent killers, null and undefined! Handle these demons with care to avoid pandemonium:

function isEmpty(value) { return value == null || (typeof value === 'object' && Object.keys(value).length === 0); // Because who knows what lurks in the shadows? (¬‿¬) }

Library vs native: the great trade-off debate

Never lose sight of the balancing act between efficiency, simplicity, browser support. It's a circus out there! And as always, stay updated with the latest browser support data for ECMAScript features - don't get left behind!