How to Check if a JavaScript Object is Empty: 5 Working Methods

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

"Is the object empty — or not?" Let's tackle the eternal headache of JavaScript developers

\n\n

Imagine: you're writing an application, you get a response from an API, and you need to figure out — did data come through? Or is the object empty, and it's time to show the user a placeholder? Or you're processing a form and need to check if the user filled in at least one field? This situation is painfully familiar to anyone who writes in JavaScript.

\n\n

It seems like it couldn't be simpler, right? if (obj === {}) — and you're done? Alas, no. In JavaScript, two objects, even with absolutely identical content, will never be equal to each other unless they are the same object in memory. Checking for an empty JavaScript object is a task that has garnered thousands of views on Stack Overflow and spawned an entire industry of solutions.

\n\n

In this article, we'll break down 5 reliable ways to determine if you're dealing with an empty object. From classic to modern, from fast to safe. Let's go!

\n\n

What does "empty object" mean in JavaScript?

\n\n

Before diving into the code, let's agree on terms. An empty object in JavaScript is an object that has no own enumerable properties. That is, {} is empty. But { name: "Alice" } is not.

\n\n

An important nuance: any object has a prototype (Object.prototype), and therefore it has inherited properties (e.g., toString, hasOwnProperty). We are not interested in those. We only check what we ourselves have added.

\n\n

Another pitfall is null. If a variable equals null or undefined, we cannot even call methods on it. Therefore, the first thing we always do is check that the object exists.

\n\n

Method 1: Object.keys() — the gold standard

\n\n

The most popular and recommended method in modern JavaScript. The Object.keys() method returns an array of an object's own enumerable properties. If the array is empty, the object is empty.

\n\n
function isEmpty(obj) {\n    return Object.keys(obj).length === 0;\n}\n\n// Usage examples\nconsole.log(isEmpty({}));           // true\nconsole.log(isEmpty({a: 1}));       // false\nconsole.log(isEmpty({name: 'John', age: 30})); // false
\n\n

Why does this work? Object.keys() ignores inherited properties and returns only those that belong to the object itself. Checking an object for emptiness using Object.keys() is clean, readable, and fast code.

\n\n

Important: This method does not check whether the passed value is an object. If you pass a string or a number, it will return an empty array, and we might mistakenly conclude the object is empty. Therefore, for reliability, we add a type check:

\n\n
function isEmptySafe(obj) {\n    return obj && typeof obj === 'object' && Object.keys(obj).length === 0;\n}\n\nconsole.log(isEmptySafe('hello')); // false (string is not an object)\nconsole.log(isEmptySafe(null));    // false\nconsole.log(isEmptySafe({}));      // true
\n\n

Method 2: JSON.stringify() — unexpected but it works

\n\n

Sometimes programmers jokingly call this method a "crutch," but it works. The idea is simple: serialize the object into a JSON string and compare it to '{}'.

\n\n
function isEmptyJSON(obj) {\n    return JSON.stringify(obj) === '{}';\n}\n\nconsole.log(isEmptyJSON({}));           // true\nconsole.log(isEmptyJSON({key: 'val'})); // false
\n\n

Why is this method not ideal? First, JSON.stringify() ignores functions, undefined, and symbols. If an object has a property with the value undefined, it will simply disappear during serialization, and the object will be considered empty, even though it actually has a key.

\n\n
const obj = { a: undefined };\nconsole.log(isEmptyJSON(obj)); // true — but it should be false!
\n\n

Second

Blogs

Book Recommendations