The "delete" in JavaScript

Search for a command to run...

😊
(This article was primarily written for another blog. The language of this article is more formal. Please let me know your thoughts through the comments.) Writing better and high-quality code is essential. Better code makes software stable and seamle...

This article is going to be a fun one. In this article, we'll be building a Tic Tac Toe game just by using HTML, CSS, and JavaScript. This is going to be an easy one. Both the inputs have to be placed by the user. But in another article, we will be c...

I am sure at some point in your life while browsing the web, you have found a web page that shows an animated counter. A counter that starts from 0 and goes all the way up to some given number. Here is an example of how such a counter looks. In this...

Building a JavaScript project always gives an excellent overview of the internals of the language. And also, it helps to make the portfolio better. In today’s article, we will be building a fantastic mini JavaScript project. We aim to develop a Passw...

Building a Weather app with JavaScript is an excellent project for beginners. It helps to understand the core basics of the DOM and teaches how to use fetch API, to call and get data from a third-party service. In this tutorial, we'll be building a g...

In this article, we are going to discuss the delete operator in JavaScript. Delete is comparatively a lesser-known operator in JavaScript. We use this operator mainly to delete JavaScript object properties.
JavaScript has pop(), shift() or splice() methods to delete an element from an array. But because of the key-value pair in an object, the deleting is more complicated in case of objects. It is important to note that, the delete operator only works on objects and not on variables or functions.
delete object
// Or
delete object.property
// Or
delete object['property']
The operator returns true if it removes a property. While deleting an object property that doesn't exist will return a true but it will not affect the object in any way. Though while trying to delete a variable or a function will return a false.
Let's assume we have an object called person which has three key-value pairs (i.e., firstName, lastName and phone). Now, we'll use the delete operator to delete the phone property from the object.
let person = {
firstName: "John",
lastName: "Doe",
phone: 12345
}
console.log(delete person.phone); //true
console.log(person); //{ firstName: 'John', lastName: 'Doe'}

As the above output shows, delete person.phone returns true and if we log the person object we can clearly see that the phone property doesn't exist anymore.
Let's try applying the delete operator for deleting a variable and a function.
let num = 5;
let sum = (a, b) => {
return a + b;
}
console.log(delete num); //false
console.log(delete sum); //false

Because the delete operator doesn't work for variables or function, it returns false and the actual variables remain untouched.
Another thing to keep in mind is that this operator doesn't delete the value of the property rather it deletes the property itself.
let person = {
firstName: "John",
lastName: "Doe",
phone: 12345
}
let phone = person.phone;
console.log(delete person.phone); //true
console.log(phone); //12345
Because objects are reference types, so both the person.phone and phone variable will refer to the same memory address.

It is clearly visible that the delete operator has deleted the property but the value is still in the memory which shows that, delete doesn't delete the value.
Global variables can be removed using the delete operator. Because the global variables are properties of the window object. And as delete works on objects, it'll delete the variable.
toDelete = 5;
console.log(delete toDelete); //true
console.log(toDelete); //toDelete is not defined
Without using the var, let or const keyword sets the variable as a global variable. And it'll work as an object property.

The delete toDelete returns true and if we try to access the variable after deleting it we'll get a reference error as the variable is not defined.
deleteAs we know JavaScript arrays are after-all objects. So, the delete operator can be used to delete an array item. But it'll cause a problem because after deleting the element from the array, this operator will show the position as empty and it'll not update the array length also.
let arr = [1, 2, 3]
console.log(delete arr[0]); //true
console.log(arr); //[empty, 2, 3]

So, using pop(), shift() or splice() methods are clearly a better approach to delete array elements.
Developers uses many workarounds to delete an object property, such as setting the value of an object property to null or undefined. But the property will still exist on the object and some operators like for in loop will still show the presence of the null property which can cause problems in many cases.
Using the delete property in loops also slows down a program significantly. So, this method should only be used when it is absolutely necessary to delete an object property without leaving any traces.