🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Object.keys, Object.entries, Object.values, and for...in loop.Table of Contents
- Introduction
- Using
Object.keysMethod - Using
Object.entriesMethod - Using
Object.valuesMethod - Using
for...inLoop - Conclusion
Introduction
In JavaScript, an object is considered empty if it does not have any enumerable properties. There are several ways to check for this condition, and each method has its own advantages.
Using Object.keys Method
The Object.keys method returns an array of a given object's own enumerable property names. If the length of this array is 0, the object is empty.
Syntax
Object.keys(object).length === 0
Example
const emptyObject = {};
const nonEmptyObject = { name: "Ravi" };
console.log(Object.keys(emptyObject).length === 0); // true
console.log(Object.keys(nonEmptyObject).length === 0); // false
Using Object.entries Method
The Object.entries method returns an array of a given object's own enumerable string-keyed property [key, value] pairs. If the length of this array is 0, the object is empty.
Syntax
Object.entries(object).length === 0
Example
const emptyObject = {};
const nonEmptyObject = { name: "Sita" };
console.log(Object.entries(emptyObject).length === 0); // true
console.log(Object.entries(nonEmptyObject).length === 0); // false
Using Object.values Method
The Object.values method returns an array of a given object's own enumerable property values. If the length of this array is 0, the object is empty.
Syntax
Object.values(object).length === 0
Example
const emptyObject = {};
const nonEmptyObject = { name: "Arjun" };
console.log(Object.values(emptyObject).length === 0); // true
console.log(Object.values(nonEmptyObject).length === 0); // false
Using for...in Loop
The for...in loop iterates over all enumerable properties of an object. If the loop does not execute any iterations, the object is empty.
Syntax
function isEmpty(object) {
for (let key in object) {
if (object.hasOwnProperty(key)) {
return false;
}
}
return true;
}
Example
const emptyObject = {};
const nonEmptyObject = { name: "Lakshmi" };
console.log(isEmpty(emptyObject)); // true
console.log(isEmpty(nonEmptyObject)); // false
Conclusion
Checking if an object is empty in JavaScript can be accomplished using various methods, including Object.keys, Object.entries, Object.values, and the for...in loop. Each method has its own advantages and specific use cases:
- The
Object.keysmethod is straightforward and commonly used. - The
Object.entriesandObject.valuesmethods are similar toObject.keysand can be used based on preference or specific requirements. - The
for...inloop is useful if you need to check for properties directly on the object and not inherited properties.
By understanding these methods, you can choose the most appropriate one for your specific use case when working with objects in JavaScript.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment