About this question
How do you test whether a JavaScript object is empty? What methods or built-in functions can you use to check if an object has no properties?
How do you test whether a JavaScript object is empty? What methods or built-in functions can you use to check if an object has no properties?
Log in to share your answer and help other learners.
Log in to answerBest Answer · By JanBask Java Expert
Answered on Aug 27, 2025
In JavaScript, objects are widely used to store key-value pairs, but sometimes you need to check if an object is empty—that is, if it has no properties. This leads to the question: “How do I test for an empty JavaScript object?” Thankfully, there are several simple approaches.
Method 1: Using Object.keys()
The most common way is to check the length of the keys array:
let obj = {};
console.log(Object.keys(obj).length === 0); // true If there are no keys, the object is empty.
Method 2: Using Object.entries() or Object.values()
Both return arrays, so you can also check their length:
console.log(Object.entries(obj).length === 0); // trueMethod 3: Using JSON.stringify()
Convert the object to a string and compare with {}:
console.log(JSON.stringify(obj) === "{}"); // true Not the most efficient, but useful for quick checks.
Method 4: Custom Utility Function
For reusability, you can write:
function isEmpty(obj) {
return Object.keys(obj).length === 0;
}Important Note:
These methods only check for own properties, not inherited ones. If you need a strict check, use:
Object.getOwnPropertyNames(obj).length === 0; Key Takeaway:
The most reliable and efficient way to test for an empty object in JavaScript is to check if Object.keys(obj).length === 0. This ensures you’re only testing the object’s own enumerable properties.
Lily Smith Latest answer
Answered on Oct 1, 2026
Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.
Most-read guides across JanBask
Common Java interview questions, answered
Guides, tips and career advice on Java from JanBask experts.
Java Frequently Asked J2EE Interview Questions and Answers You Must Read
Top 45 frequently asked Java J2EE interview questions to ace your interview. These J2EE questions are useful for freshers and…
Java Top 100+ Angular Interview Questions and Answers
Are you preparing for your next Angular interview? We have compiled the list of the top 100+ Angular Interview Questions &…
Java Top 51+ Simple Java Project Ideas for Beginners In 2025
Enhance your Java skills with our top 51+ project ideas! From beginner-friendly exercises to advanced applications, fuel your…
Java How Long Does It Takes To Learn Java?
Confused about How long does it take to learn Java ? Well, No worries! This blog will guide you with valuable information and…