JavaScript delete Operator Example

📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.

🎓 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 (176K+ subscribers): Java Guides on YouTube

▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube

The JavaScript delete operator removes a property from an object; if no more references to the same property are held, it is eventually released automatically.

Example 1 - Delete a property from an object

 // using Object Literals var user = { firstName : 'Ramesh', lastName : 'Fadatare', emailId : 'ramesh@gmail.com', age : 29, getFullName : function (){ return user.firstName + " " + user.lastName; } } function testDeleteOperator(){ console.log("before delete user object properties -> " + JSON.stringify(user)); // delete properties delete user.firstName; delete user.age; // delete functions delete user.getFullName(); console.log("after delete user object properties -> " + JSON.stringify(user)); } testDeleteOperator();
Output:
before delete user object properties -> {"firstName":"Ramesh","lastName":"Fadatare","emailId":"ramesh@gmail.com","age":29} after delete user object properties -> {"lastName":"Fadatare","emailId":"ramesh@gmail.com"}

Example 2

The following snippet gives a simple example:
var Employee = { age: 28, name: 'abc', designation: 'developer' } console.log(delete Employee.name); // returns true console.log(delete Employee.age); // returns true // When trying to delete a property that does  // not exist, true is returned  console.log(delete Employee.salary); // returns true

Testing

For the best learning experience, I highly recommended that you open a console (which, in Chrome and Firefox, can be done by pressing Ctrl+Shift+I), navigate to the "console" tab, copy-and-paste each JavaScript code example from this guide, and run it by pressing the Enter/Return key.

You can refer below screenshot for your testing:



Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare