JavaScript Delete Operator

This article will discuss the delete operator available in JavaScript. Relative deletion is a lesser-known operator in JavaScript. This operator is more specifically used to remove properties from JavaScript objects.

 The JavaScript methods pop(), shift(), or splice() are available to remove an element from an array.
But because there are key-value pairs in an object, deletion is more complicated.

Note – The delete operator only works on objects and not on variables or functions.

Syntax:

delete object
// or
delete object.property
// or
delete object['property']

Parameter: It does not take any parameter.

Return type: This operator returns true if it removes an attribute. When deleting a non-existing object property, it returns true but does not affect the object. However, attempting to remove a variable or function will return an incorrect value.

Below are examples of the delete Operator.

Example: JavaScript

let emp = { 
    firstName: "Raj"
    lastName: "Kumar"
    salary: 40000 
 
console.log(delete emp.salary);
console.log(emp);

Output:

true
{"firstName":"Raj","lastName":"Kumar"}

Example 2: Let’s assume an object called person has three key-value pairs (i.e. first name, lastName and phone). Now we will use delete operator to delete the phone property will return true.

JavaScript

let person = {
    firstName: "John",
    lastName: "Doe",
    phone: 12345
}
 
console.log(delete person.phone);
console.log(person);

Output: 

Javascript

As the above picture shows, delete person.phone returns true and logging the person object shows that the phone property is removed, and it doesn’t exist in object.

Let’s try applying the delete operator for deleting a variable and a function.

Example 3: JavaScript

let num = 5;
let sum = (a, b) => {
    return a + b;
}
 
console.log(delete num); //false
console.log(delete sum); //false

Output:

false
false

Since the delete operator does not work on variables or functions, it returns false, and the actual variables and functions remain the same.

Another thing to note is that this operator does not remove the value of the attribute, but instead the attribute itself.

Example 4:

JavaScript

let person = {
  firstName: "John",
  lastName: "Doe",
  phone: 12345
}
 
let phone = person.phone;
 
console.log(delete person.phone); //true
console.log(phone); //12345

As objects are reference types, so both the person.phone and phone variable will refer to the same memory address.

Output:

true
12345

The output shows that the delete operator has deleted the property but the value still exists on the memory.

Exception: Global variables can be deleted using the delete operator. Because global variables are properties of the window object, and because delete operates on objects, the variable is deleted.

Example: JavaScript

toDelete = 5;
 
// true
console.log(delete toDelete);
 
// toDelete is not defined
console.log(toDelete);

Do not use var, let or const keywords, the variable is defined as a global variable and it will act as an object property.

Output:

true
Uncaught Reference Error: toDelete is not defined

The delete toDelete returns true and trying to access the variable after deleting it throws a reference error as the variable is not defined anymore.

Deleting Array Values Using delete: JavaScript arrays are ultimately objects. So, the delete operator can be used. But this will cause problem because after removing the element from the array, this operator will show empty position and will not update the length of the array.

Example: JavaScript

let arr = [1, 2, 3]
 
console.log(delete arr[0]); //true
console.log(arr); //[empty, 2, 3]

Output: 

Javascript

So, using pop(), shift(), or splice() methods is clearly a better approach to deleting array elements.

Conclusion: There are other ways that developers use, such as setting the value of an object property to null or undefined. But the property will always exist on the object, and some operators like the for in loop will always show the presence of a null or undefined property.

Using the delete property in a loop slows down the program significantly. Therefore, this method should only be used when it is absolutely necessary to remove an object property.

Whether you are preparing for your first job interview or planning to improve your skills in this ever-evolving tech landscape, CodeConfig.in Courses will help you in achieving success. We provide top-quality content on JavaScript at no cost, all geared towards accelerating your growth in the limited time period. Join the millions we have already helped, and we are here to do the same for you. Don’t miss out – check it out now!
Please Check all JavaScript articles HERE. For more such post you can refer HERE.