🎓 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
delete operator. This guide explains the different ways you can remove a property from an object and considerations when using the delete operator.1. Using the delete Operator
The most straightforward way to remove a property from an object is by using the delete operator. It removes the property and its value from the object.
Example:
const employee = {
firstName: "Ramesh",
lastName: "Fadatare",
email: "ramesh.fadatare@gmail.com"
};
// Deleting the email property
delete employee.email;
console.log(employee);
Output:
{
firstName: "Ramesh",
lastName: "Fadatare"
}
In this example, the delete operator removes the email property from the employee object. The object no longer contains the email key.
2. Deleting Properties Using Bracket Notation
You can also delete a property using bracket notation. This method is useful if the property name is stored in a variable or when the property name contains spaces or special characters.
Example:
const employee = {
firstName: "Ramesh",
lastName: "Fadatare",
email: "ramesh.fadatare@gmail.com"
};
const propertyToDelete = "email";
// Deleting the property using bracket notation
delete employee[propertyToDelete];
console.log(employee);
Output:
{
firstName: "Ramesh",
lastName: "Fadatare"
}
In this example, the property stored in propertyToDelete ("email") is deleted from the employee object.
3. The delete Operator Only Removes Own Properties
The delete operator only works on properties that are directly on the object. It won’t delete properties inherited from an object’s prototype.
Example:
const employee = Object.create({ department: "Engineering" });
employee.firstName = "Ramesh";
employee.lastName = "Fadatare";
// Attempting to delete an inherited property
delete employee.department;
console.log(employee.department); // Output: Engineering
In this example, the department property is inherited from the prototype and can't be deleted by the delete operator.
4. Checking for Property Existence Before Deleting
You can check if a property exists in an object before deleting it using the hasOwnProperty() method or the in operator.
Example:
const employee = {
firstName: "Ramesh",
lastName: "Fadatare",
email: "ramesh.fadatare@gmail.com"
};
if (employee.hasOwnProperty("email")) {
delete employee.email;
}
console.log(employee);
Output:
{
firstName: "Ramesh",
lastName: "Fadatare"
}
In this example, the hasOwnProperty() method checks if the email property exists before deleting it.
5. delete vs Setting Property to undefined or null
It's important to note that delete removes the property entirely, while setting a property to undefined or null keeps the property in the object but without a meaningful value.
Example:
const employee = {
firstName: "Ramesh",
lastName: "Fadatare",
email: "ramesh.fadatare@gmail.com"
};
// Setting email to undefined
employee.email = undefined;
console.log(employee); // Output: { firstName: "Ramesh", lastName: "Fadatare", email: undefined }
In this case, the email property still exists in the object but is now undefined.
Example of using delete:
delete employee.email;
console.log(employee); // Output: { firstName: "Ramesh", lastName: "Fadatare" }
Here, the email property is completely removed from the object.
Conclusion
To remove a property from an object in JavaScript, the delete operator is the most common and effective method. Remember that delete only removes own properties, not those inherited from a prototype. If you need the property to remain but with no value, you can set it to undefined or null instead of using delete.
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