JavaScript

JavaScript Delete Operator

The delete operator is utilized to remove an existing property of any object. It is only effective on the properties of the object. The delete operator has its own importance for deleting a specific property in a complex data structure. Moreover, the property does not affect the function names or variables. In this article, the working and usage of the delete operator in different scenarios are demonstrated.

How to Utilize the Delete Operator in JavaScript?

The delete operator is used to delete the properties of objects in JavaScript. The operator returns the true value after completing the action; otherwise, it returns false. It is useful to access the specific property of an object and remove it from the existing property. However, the performance of the delete operator varies from one scenario to another.

  • If the property does not exist and the user utilizes the delete operator to delete it, the operator returns the true value.
  • A property declared with the var keyword cannot be deleted from the method or global scope.
  • If a property is declared with const or let keywords, users are unable to delete it from the scope in which they are defined.
  • The built-in object properties such as Array, Math, and Object cannot be deleted.

Syntax

delete object.property;

Or

delete object["property"];

Parameters:

The parameters of the above syntaxes are provided below:

  • property: specify the property to be deleted.
  • object: represents the object name whose property is deleted.

Example 1

An example is adapted to delete the property by employing the delete operator in JavaScript.

Code

// An example of using the delete operator in // An example of using the delete operator in JavaScript
var Teacher = {
    name: 'John',
    age: 34,
    designation: 'professor'
  }
console.log(delete Teacher.designation);    

console.log(delete Teacher.name);  

console.log(delete Teacher.salary);

The description of the code is given below:

  • An object is created with the name of Teacher.
  • After that, different properties are utilized, such as name, age, and designation.
  • Different values, including “John”, “34” and “professor” are assigned to the above-mentioned keys.
  • Furthermore, the delete operator is utilized with the delete keyword inside of the console.log() method.
  • Finally, delete the designation property by accessing “delete Teacher.designation”. Similarly, name and salary properties are deleted with this operator.

Output

The output returns the true value that represents that the delete operator successfully deleted the name, age, and designation properties.

Example 2: Access and Delete the Property With Delete Operator

Another example is adapted to utilize the delete operator in JavaScript. For this purpose, the code is as follows:

Code

// An example of using the delete operator in JavaScript
const car = {
    model: 'BMW',
    color: 'Honda'
  }
console.log( car.model)
delete car.model
console.log( car.model)

console.log( car.color)
delete car['color']
console.log( car.color)

The description of the code is given below:

  • First, a car object is created and different properties like model and color are defined in it.
  • After that, BMW and Honda values are assigned to these properties.
  • Furthermore, the model property of the car object is accessed and deleted using the delete car.model.
  • The same procedure is repeated for the color property in JavaScript.

Output

The outcome of the code represents that the model and color properties of the object car are deleted and returned undefined.

Conclusion

The delete operator is utilized to delete the existing property of an object In JavaScript. In this article, an overview of the delete operator is demonstrated with different scenarios. Moreover, various examples are provided to practically implement the delete operator in JavaScript. The operator is mostly used to delete a specific property in a complex data structure.

About the author

Syed Minhal Abbas

I hold a master's degree in computer science and work as an academic researcher. I am eager to read about new technologies and share them with the rest of the world.