JavaScript delete Operator Tutorial

In this section, we will learn what the delete operator is and how to use it in JavaScript.

JavaScript Remove Property from Object

The delete operator is used when we have an object and want to remove a property from that object.

Notes:

  • We can’t use the delete operator on variables and functions in a program. It is only used for properties of an object.
  • The delete operator removes both the property and its value of an object.
  • Don’t use this operator on the built-in objects of JavaScript! It might crash your program as a result.

JavaScript delete Operator Syntax:

delete object.propertyName;

JavaScript delete Operator Input Value:

This operator only takes one argument, and that is the property of the target object we want to remove. And we put this value on the right side of the delete operator.

Example: removing Property from Object in JavaScript

const emp = {
  id: 123, 
  position: "Manager",
  name: "John", 
  lastName: "Doe"
}
delete emp.id; 
delete emp.position; 
console.log(emp);

Output:

{name: "John", lastName: "Doe"}

As you can see, the `emp` object had 4 properties at first but after calling the `delete` operator on the `id` and `position` properties, the object is left only with the `name` and `lastName` properties.

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies