The delete operator
The delete operator in JavaScript removes properties from objects (or elements from arrays). It removes the property itself, not only its value, and it has several subtleties: it does not shift array elements, it does not touch the prototype chain, and it does not work on variables.
Theory
TL;DR
- Syntax:
delete object.propertyordelete object['property']. - It removes the key itself: after
delete,'key' in objisfalse. - It returns
trueon success andfalsewhen the property is non-configurable. - In an array it leaves a hole:
lengthdoes not change and elements are not shifted. - It does not remove properties inherited through the prototype chain, only own ones.
- It does not work on
var,letorconstvariables; in strict mode a failed removal throws aTypeError.
Quick example
const obj = { a: 1, b: 2 };
delete obj.a;
console.log(obj); // { b: 2 }
console.log('a' in obj); // falseSyntax and an object example
delete object.propertyor
delete object['property']Example:
const user = {
name: 'Alice',
age: 25
};
delete user.age;
console.log(user); // { name: 'Alice' }
console.log('age' in user); // falseThe
deleteoperator removes the property itself from the object, not just its value.
Return value
delete returns a boolean:
| Case | Returns |
|---|---|
| The removal succeeded | true |
| Removal is impossible (for example, the property is non-configurable) | false |
Example:
const obj = {};
Object.defineProperty(obj, 'x', { value: 10, configurable: false });
console.log(delete obj.x); // false
console.log(obj.x); // 10Note that delete also returns true when the property did not exist at all, because after the operation the key is definitely absent.
Behaviour with arrays
const arr = [10, 20, 30];
delete arr[1];
console.log(arr); // [10, <1 empty item>, 30]
console.log(arr.length); // 3Important:
deletedoes not shift array elements, it simply removes the value and leaves a hole (empty).
If you need to remove an element and change the array length, use methods instead:
arr.splice(1, 1); // removes the element and shifts the restA sparse array left by delete behaves inconsistently: forEach, map and filter skip the holes, while for and for...of see undefined.
delete versus assigning undefined
The difference between delete and assigning undefined:
const user = { name: 'Tim', age: 25 };
user.age = undefined;
console.log(user); // { name: 'Tim', age: undefined }
console.log('age' in user); // true (the key is still there)
delete user.age;
console.log(user); // { name: 'Tim' }
console.log('age' in user); // false (the key is removed)
deleteremoves the key completely, while assigningundefinedonly clears the value. The difference shows up inObject.keys(),JSON.stringify()and in afor...inloop.
Prototypes, limitations and summary
delete does not touch properties inherited through the prototype chain:
const person = { name: 'Alex' };
const user = Object.create(person);
console.log('name' in user); // true
delete user.name; // will not remove it from the prototype
console.log('name' in user); // trueIt removes only the object's own properties.
Limitations:
-
deletedoes not work on variables declared withvar,letorconst:javascriptlet x = 10; console.log(delete x); // false console.log(x); // 10Only an object property can be deleted, never a variable.
-
In strict mode (
"use strict") an attempt to delete a property that cannot be deleted throws:javascript"use strict"; delete Object.prototype; // TypeError
Summary table:
| Question | Answer |
|---|---|
| What it does | Removes a property from an object |
| What it works with | Objects, arrays |
| What it returns | true or false |
| What it does not work for | Variables (var, let, const) |
| Arrays | Does not shift elements, leaves a hole |
| Safe for | An object's own properties |
Common mistakes
- Using
deleteto remove an array element. The length stays the same and a hole is left behind; usespliceorfilter. - Confusing
delete obj.keywithobj.key = undefined: in the second case the key remains and still shows up inObject.keys(). - Expecting
deleteto remove an inherited property. You have to delete it from the object that actually owns it. - Reading the result of
deleteas "did the property exist": it returnstruefor a missing key as well. - Forgetting that in strict mode a failed removal throws a
TypeErrorinstead of quietly returningfalse. - Deleting keys in bulk in hot code: it changes the object's hidden class and slows it down; creating a new object with rest destructuring is often better.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.