Sitemap
InterviewPro

InterviewPro

delete operator in JavaScript

3 min readMar 24, 2025

--

In JavaScript, removing properties from objects and elements from arrays is a common task. The delete operator is a popular tool for this, but it's important to understand how it works and when to use it to avoid common mistakes and performance problems.

This article will explain what the delete operator does, how it works, when to use it, and some best practices for efficient and reliable code.

Press enter or click to view image in full size

What is JavaScript delete operator?

The delete operator is used to remove properties from objects and elements from arrays. When applied to an object, it removes the specified property, and when applied to an array, it deletes the element at the specified index.

Get InterviewPro’s stories in your inbox

Join Medium for free to get updates from this writer.

Syntax:

delete object.property;

How Does the delete Operator Work?

Deleting Object Properties

The behavior of the delete operator varies slightly depending on whether you use it on an object or an array.

let emp = { name: "John", age: 30 };
// Removes the 'age' property
delete emp.age;
console.log(emp);
// Output: { name: "John" }

In this case, the age property is removed from the person object, and the object is left with only the name property.

Deleting Array Elements

When you use the delete operator on an array, the element at the specified index is removed. However, the array will not be re-indexed, and the deleted index will remain as undefined. This is important to note, as it can lead to unintended gaps in the array.

let arr = [10, 20, 30, 40, 50 ];
delete arr[1]; // Deletes the element at index 1
console.log(arr); // Output: [10, undefined, 30, 40, 50]

While the element is technically deleted, the array still has the same length, and the index where the element was deleted remains undefined.

Performance

Although the delete operator is useful, it can have performance implications, especially when used repeatedly in loops or when working with large objects. Here's why:

  • Deleting properties from objects can cause the internal structure of the object to change, which may lead to slower performance in some JavaScript engines.
  • Deleting elements from arrays can result in sparse arrays, which are less efficient in terms of iteration and indexing.

For better performance, consider the following alternatives:

  • Avoid Deleting Array Elements: Instead of using delete on an array, use methods like splice() to remove elements, which maintains array indexing.
let arr = [10, 20, 30, 40, 50];
arr.splice(1, 1); // Removes the element at index 1 and shifts the array
console.log(arr); // Output: [10, 30, 40, 50]
  • Set to null or undefined: Instead of deleting object properties, you can set them to null or undefined to clear their values without removing the property entirely.
let emp = { name: "John", age: 25 };
emp.age = null; // Clears the value of 'age' but doesn't delete the property
console.log(emp); // Output: { name: "John", age: null }

Conclusion

The JavaScript delete operator is a powerful tool for removing properties from objects and elements from arrays. Understanding how it works, its limitations, and the potential pitfalls can help you avoid common mistakes and write more efficient code. While the delete operator can be useful for memory management and cleaning up unnecessary data, always consider alternatives like splice() for arrays or setting properties to null when you don’t need to delete them completely.

By following best practices and considering performance, you can leverage the delete operator effectively in your JavaScript development projects.

If you found this article helpful, don’t forget to follow InterviewPro for more content on JavaScript tips and best practices!

Show your support on my YouTube channel too 😁.

--

--