Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

252
Views
How to tell if a JavaScript value is an empty object?

I came across this issue while debugging an app, and the other answers do not solve this!!

With the following use case :

class Foo {
  get foo() { return 'foo'; }
}

const value = new Foo();

The following popular solutions do not work :

!Object.keys(value).length;                   // true
!Object.entries(value).length;                // true
!Object.getOwnPropertyNames(value).length;    // true

Neither is this solution :

function isEmpty(obj) {
  for (var prop in obj) {
    return false;
  }
  return true;
}
isEmpty(value);                              // true

To be clear, Foo has a getter, and should not be considered empty!

In other words, I expect these results from these use cases :

const value1 = new class {};                              // value1 is empty
const value2 = new class { get foo() { return 'foo'; } }  // value2 is not empty
const value3 = {};                                        // value3 is empty
const value4 = { foo:'foo' };                             // value4 is not empty
const value5 = Object.create(null);                       // value5 is empty
const value6 = Object.create(null); value6.foo = 'foo';   // value6 is not empty

How can this be evaluated reliably?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

According to MDN:

When using get the property will be defined on the instance's prototype

In your Foo example:

class Foo {
  get foo() { return 'foo'; }
}

const value = new Foo();

console.log(Object.getOwnPropertyNames(Object.getPrototypeOf(value)));
// check if 'empty' (has only "constructor")
console.log(Object.getOwnPropertyNames(Object.getPrototypeOf(value)).length === 1);

If you want a function that covers all example cases, you can use this:

function isEmpty(obj) {
  const proto = Object.getPrototypeOf(obj);
  return (Object.keys(obj).length === 0) && (
    proto === null
    || proto === Object.prototype
    || (Object.getOwnPropertyNames(proto).length === 1)
  );
}

const value1 = new class {};                              // value1 is empty
const value2 = new class { get foo() { return 'foo'; } }  // value2 is not empty
const value3 = {};                                        // value3 is empty
const value4 = { foo:'foo' };                             // value4 is not empty
const value5 = Object.create(null);                       // value5 is empty
const value6 = Object.create(null); value6.foo = 'foo';   // value6 is not empty

[value1, value2, value3, value4, value5, value6]
  .map((v, i) => `value${i + 1} is ${isEmpty(v) ? "empty" : "not empty"}`)
  .forEach((v) => console.log(v));

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!