delete in JavaScript: removing a property from an object

The delete operator removes a property from an object. What it returns, why it damages arrays, and the alternatives worth reaching for instead.
3 min read
Believemy logo

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.

JAVASCRIPT
const customer = { name: 'Marie', password: 'secret' };

delete customer.password;

console.log(customer);               // { name: 'Marie' }
console.log('password' in customer); // false

The 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.

Good to know

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.

JAVASCRIPT
const grades = [12, 15, 8];

delete grades[1];

console.log(grades.length); // 3, unchanged
console.log(1 in grades);   // false
console.log(grades[1]);     // undefined

The 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 needRecommended form
Remove a field without changing the objectconst { password, ...rest } = customer
Remove an element from an arraygrades.filter((n, i) => i !== 1)
Remove dynamic keys oftenA 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

Question

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.


Question

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.


Question

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.

Related terms

Discover our javaScript glossary

Every word of JavaScript explained simply: keywords, built-in objects, methods, errors and concepts. Clear definitions and examples that actually run, to learn and to troubleshoot.

Share this article

Want to help us? Share this article on your networks or even better: on your site, in an article or in your newsletter.