Back to JavaScript
2026-03-056 min read

delete (JavaScript)

Learn delete (JavaScript) step by step with clear examples and exercises.

Why This Matters

In this full guide, we'll delve into the delete operator of JavaScript, a powerful tool that allows you to remove properties from objects. This operator is crucial for understanding advanced object manipulation and can be used in real-world scenarios such as debugging, optimizing memory usage, and preparing for interviews.

By mastering the delete operator, you'll gain control over your JavaScript objects, making it easier to manage complex data structures and write more efficient code.

Prerequisites

Before diving into the delete operator, it's essential to have a solid grasp of the following concepts:

  1. JavaScript basics (variables, data types, operators, functions)
  2. Objects and properties in JavaScript
  3. Understanding how objects are stored in memory
  4. Familiarity with arrays and their properties
  5. Understanding the concept of object prototypes
  6. Knowledge about the in operator for checking property existence
  7. Understanding the difference between primitive values and objects
  8. Comprehension of the typeof operator to identify data types
  9. Familiarity with JavaScript's memory management (Garbage Collection)
  10. Understanding the concept of object destructuring

Core Concept

The delete operator in JavaScript is used to remove a property from an object. Here's the syntax:

delete object.property;
delete object[property];

Both forms can be used interchangeably, but the second form allows for more flexibility with dynamic properties.

When you delete a property, it no longer appears in the object's list of properties, and accessing that property will return undefined. However, if the deleted property's value was an object and there are no more references to that object, the object held by that property is eventually released automatically. This can help optimize memory usage in large applications.

Example 1 - Removing a property from an object

const employee = {
firstName: "Maria",
lastName: "Sanchez",
};

console.log(employee.firstName); // Output: Maria
delete employee.firstName;
console.log(employee.firstName); // Output: undefined

Example 2 - Removing a property from an array

const fruits = ["apple", "banana", "orange"];
console.log(fruits[1]); // Output: banana
delete fruits[1];
console.log(fruits[1]); // Output: undefined

Example 3 - Removing a property from an object with dynamic keys

const person = {
"first-name": "John",
"last-name": "Doe",
};
console.log(person["first-name"]); // Output: John
delete person["first-name"];
console.log(person["first-name"]); // Output: undefined

Worked Example

Let's consider a scenario where we have an object representing a user with their name, email, and password. We want to remove the password property for security reasons.

const user = {
name: "John Doe",
email: "johndoe@example.com",
password: "secret123",
};

// Remove the password property for security reasons
delete user.password;
console.log(user);
/* Output:
{
name: "John Doe",
email: "johndoe@example.com"
}
*/

Common Mistakes

  1. Trying to delete a non-existent property: If you try to delete a property that doesn't exist in the object, it will not throw an error but simply do nothing.
  1. Deleting a property that is part of an object's prototype chain: Deleting properties from objects created via Object.create() or properties inherited from the Object.prototype can lead to unexpected behavior. It is generally recommended to avoid deleting such properties.
  1. Not understanding the impact on memory: While deleting a property removes it from the object, if the deleted property's value was an object with other references, the object will not be garbage collected until all references are removed.
  1. Attempting to delete a property of a primitive value (number, string, boolean): The delete operator only works with objects in JavaScript.
  1. Deleting properties from array-like objects (e.g., NodeList, Array-like Strings) that are not true arrays: These objects do not support the delete operator because they are not true arrays, but rather object representations of collections.
  1. Using the delete operator on read-only properties: If a property is marked as readOnly, you will receive an error when trying to delete it.
  1. Deleting properties in strict mode: In strict mode ("use strict"), deleting undefined properties will throw an error.

Practice Questions

  1. Given the following object, delete the age property and log the updated object:
const person = {
name: "John",
age: 30,
};

Answer:

delete person.age;
console.log(person); // Output: {name: "John"}
  1. Explain what happens when you delete the length property of an array in JavaScript.
  1. Given the following object, delete the city property and log the updated object:
const address = {
street: "123 Main St",
city: "New York",
state: "NY",
zipCode: 10001,
};

Answer:

delete address.city;
console.log(address); // Output: {street: "123 Main St", state: "NY", zipCode: 10001}
  1. Given the following object, delete the employees array and log the updated object:
const company = {
name: "TechCorp",
employees: [
{ name: "Alice", role: "Developer" },
{ name: "Bob", role: "Designer" },
],
};

Answer:

delete company.employees;
console.log(company); // Output: {name: "TechCorp"}
  1. Explain the difference between deleting a property and setting its value to undefined.

FAQ

  1. What happens if I try to delete a property that is part of an object's prototype chain? Deleting properties from objects created via Object.create() or properties inherited from the Object.prototype can lead to unexpected behavior. It is generally recommended to avoid deleting such properties.
  1. Can I use the delete operator on primitive values like numbers and strings? No, the delete operator only works with objects in JavaScript.
  1. What happens when I delete a property whose value is an object with other references? The object will not be garbage collected until all references are removed. Be careful when deleting properties that hold important data.
  1. Why can't I use the delete operator on array-like objects like NodeList or Array-like Strings? These objects do not support the delete operator because they are not true arrays, but rather object representations of collections.
  1. What is the difference between deleting a property and setting its value to undefined? Deleting a property removes it entirely from the object, while setting its value to undefined leaves the property in the object but sets its value to undefined. The choice between the two depends on your specific use case.
  1. What is the impact of deleting properties on memory usage? Deleting a property can help optimize memory usage by removing unnecessary data from objects, especially when dealing with large applications or objects containing many properties. However, be aware that if the deleted property's value was an object with other references, the object will not be garbage collected until all references are removed.
  1. What is the effect of deleting a property on object size? Deleting a property does not immediately reduce the size of the object in memory, but it can help optimize memory usage by removing unnecessary data from objects, especially when dealing with large applications or objects containing many properties.
  1. Can I delete properties using a loop? Yes, you can delete properties using loops, such as for...in or for...of. However, be aware that the for...in loop will iterate over all enumerable properties of an object, including those inherited from the prototype chain.
  1. What happens when I try to delete a property in strict mode? In strict mode ("use strict"), deleting undefined properties will throw an error. To avoid this, you can check for the existence of the property before attempting to delete it using the in operator or the hasOwnProperty() method.
  1. What is the best practice when working with sensitive data like passwords? When dealing with sensitive data like passwords, it's crucial to follow security best practices such as hashing and salting passwords before storing them, never storing plain text passwords, and using secure communication channels for transmitting sensitive data. In this example, we are demonstrating the removal of a property for educational purposes only and not promoting insecure practices.
delete (JavaScript) | JavaScript | XQA Learn