Sling Academy
Home/JavaScript/JavaScript: 3 Ways to Check if an Object is Empty

JavaScript: 3 Ways to Check if an Object is Empty

Last updated: March 14, 2023

This concise, straight-to-the-point article shows you a couple of different ways to check whether a given object is empty or not in JavaScript.

Using the Object.keys(), Object.values(), or Object.entries() method

One common approach is to use the Object.keys() method, which returns an array of the object’s own property names. You can then check if the length of this array is zero, which means the object has no keys and is empty.

Example:

const obj1 = {}
const obj2 = {name: 'Sling Academy'}

if(Object.keys(obj1).length === 0){
    console.log('obj1 is empty')
} else {
    console.log('obj1 is not empty')
}

if(Object.keys(obj2).length === 0){ 
    console.log('obj2 is empty')
} else {
    console.log('obj2 is not empty')
}

Output:

obj1 is empty
obj2 is not empt

Likewise, you can call Object.values() or Object.entries(), which return an array of the object’s own property values or key-value pairs, then examine the length of the resulting array.

Using JSON.stringify()

The JSON.stringify() method converts a JavaScript object into a JSON string. If an object is empty, the resulting string will be {}.

Example:

const isEmptyObject = (obj) => {
  return JSON.stringify(obj) === '{}';
};

// try it out
const obj = {};
console.log(isEmptyObject(obj)); 

Output:

true

Using a for…in loop

A for…in loop can be used to iterate over the object’s own and inherited enumerable properties. You can then use a flag variable to track if any property is found and break out of the loop as soon as one is found.

Example:

const user = {};
let isEmpty = true;
for (let key in user) {
  isEmpty = false;
  break;
}

console.log(isEmpty); 

Output:

true

That’s it. Goodbye!

Next Article: JavaScript: Ways to Create New Objects from Old Objects

Previous Article: The Modern JavaScript Objects Cheat Sheet

Series: Working with Objects in JavaScript

JavaScript

You May Also Like

  • Handle Zoom and Scroll with the Visual Viewport API in JavaScript
  • Improve Security Posture Using JavaScript Trusted Types
  • Allow Seamless Device Switching Using JavaScript Remote Playback
  • Update Content Proactively with the JavaScript Push API
  • Simplify Tooltip and Dropdown Creation via JavaScript Popover API
  • Improve User Experience Through Performance Metrics in JavaScript
  • Coordinate Workers Using Channel Messaging in JavaScript
  • Exchange Data Between Iframes Using Channel Messaging in JavaScript
  • Manipulating Time Zones in JavaScript Without Libraries
  • Solving Simple Algebraic Equations Using JavaScript Math Functions
  • Emulating Traditional OOP Constructs with JavaScript Classes
  • Smoothing Out User Flows: Focus Management Techniques in JavaScript
  • Creating Dynamic Timers and Counters with JavaScript
  • Implement Old-School Data Fetching Using JavaScript XMLHttpRequest
  • Load Dynamic Content Without Reloading via XMLHttpRequest in JavaScript
  • Manage Error Handling and Timeouts Using XMLHttpRequest in JavaScript
  • Handle XML and JSON Responses via JavaScript XMLHttpRequest
  • Make AJAX Requests with XMLHttpRequest in JavaScript
  • Customize Subtitle Styling Using JavaScript WebVTT Integration