An object coming back from an API holds a field you would rather not pass along, a temporary password or an internal identifier. It has to be removed, not merely emptied: a key set to undefined stays in the object and shows up in most inspections.
delete is the only operator in the language able to make a key disappear for good.
Definition
delete removes a property from an object. The key disappears along with its value, and the operator returns a boolean telling you whether the object no longer carries that property.
const customer = { name: 'Marie', password: 'secret' };
delete customer.password;
console.log(customer); // { name: 'Marie' }
console.log('password' in customer); // falseThe difference from setting the field to undefined shows immediately: after customer.password = undefined, the key still appears in Object.keys() and the in operator answers true.
delete returns true even when the property never existed. So the return value does not mean "something was removed", it means "the property is not there any more". It only gives false, or raises an error inside a module, on a protected property, after an Object.freeze() for instance.
On an array, it is a bad idea
An Array is an object, so the operator works there too. The result is surprising: the slot is emptied, but it still counts.
const grades = [12, 15, 8];
delete grades[1];
console.log(grades.length); // 3, unchanged
console.log(1 in grades); // false
console.log(grades[1]); // undefinedThe array keeps a hole, which the various iteration methods each treat in their own way. To genuinely remove an element, use splice(), which shifts the following ones, or Array.filter(), which builds a new array without touching the original.
The alternatives worth knowing
| What you need | Recommended form |
|---|---|
| Remove a field without changing the object | const { password, ...rest } = customer |
| Remove an element from an array | grades.filter((n, i) => i !== 1) |
| Remove dynamic keys often | A Map and its delete() method |
The first line relies on Rest (...) and on Destructuring: it creates an object without the named field, leaving the original untouched. It is the most common form in modern projects, where producing a copy is preferred over transforming shared data.
Frequently asked questions
Does delete free memory?
Not directly. It removes a link to a value; the engine's garbage collector is what frees the memory, and only if nothing else still points at that value. Removing a property that references a large object still used elsewhere therefore gains you nothing.
Can delete remove a variable?
No. The operator only works on an object's properties: a variable declared with const, let or var is out of its reach, and writing that inside a module raises a syntax error. To release a value, reassign the variable to null.
Is setting the property to undefined better?
It depends on what reads the object next. JSON.stringify() skips values set to undefined, so what goes out over the network will be identical. The key does stay visible to Object.keys() and to the in operator, though: if your code tests for the presence of a field, only delete gives the expected result.