How to remove properties from an object

Learn how to remove properties from JavaScript objects.

Removing properties from an object

delete The operator can be used to remove properties from an object:

Instance

var person = {
  firstName: "John",
  lastName: "Doe",
  age: 50,
  eyeColor: "blue"
};
delete person.age;  // or delete person["age"];
// Before deletion: person.age = 50, after deletion, person.age = undefined

Try it yourself

delete The operator will remove the value of the property as well as the property itself.

The property cannot be used before it is re-added.

delete The operator is intended to be used for object properties. It has no effect on variables or functions.

Note:delete Operator should not be applied to predefined JavaScript object properties. It may cause your application to crash.

Strony związane

Tutorial:Obiekt JavaScript