delete
Removes a property from an object.
Available since: ES1
Syntax
delete object.propertyExamples
Removing a property:
const user = { name: 'Anna', password: 'secret' };
delete user.password;
console.log(user);Output:
{
"name": "Anna"
}Without changing the object — destructuring with rest:
const user = { name: 'Anna', password: 'secret' };
const { password, ...publicUser } = user;
console.log(publicUser);Output:
{
"name": "Anna"
}Notes
delete is not for array elements: it leaves a hole and the length stays the same. Use splice() or filter(). Variables cannot be deleted.
See also
- in — Checks whether an object has a property (inherited ones included).
- Map.prototype.delete — Removes the entry for a key.
- Array.prototype.splice — Removes, replaces or inserts elements in the middle of an array.
In the tutorial
- Objects — Objects in JavaScript: properties, dot and bracket access, adding, deleting and nested data.