Q&A
What is the delete operator in JavaScript?
Overview
delete is specifically for removing enumerable, configurable own properties from objects. It's one of those JavaScript operators whose behavior is often misunderstood - developers expect it to work like a variable destructor, when in reality it's purely a property-removal tool for objects/arrays.
graph TD
A["delete operator"] --> B["delete obj.prop → removes property"]
A --> C["delete obj[key] → removes computed-key property"]
B --> D["Returns true on success / if prop didn't exist"]
B --> E["Returns false if prop is non-configurable"]
A --> F["delete variable → false, NO effect (in strict mode: throws)"]
A --> G["delete arr[i] → sets slot to undefined (hole), length unchanged"]
style B fill:#1e1b4b,color:#fff
Basic Object Property Removal
const user = { name: "Ada", age: 30, city: "London" };
delete user.city;
console.log(user); // { name: "Ada", age: 30 }
console.log("city" in user); // false - property is gone
After delete, the property no longer exists on the object - in returns false and hasOwnProperty returns false, unlike setting it to undefined (which keeps the property).
Bracket Notation (dynamic keys)
const key = "age";
delete user[key]; // removes the 'age' property
Return Value
delete returns true in most cases - including when the property didn't exist in the first place:
delete user.nonExistent; // true (property wasn't there, still "succeeds")
It returns false (or throws in strict mode) only for non-configurable properties:
const obj = {};
Object.defineProperty(obj, "id", { value: 1, configurable: false });
delete obj.id; // false (strict: TypeError)
What delete Does NOT Do
Variables - no effect
var x = 10;
delete x; // false - has no effect
console.log(x); // 10 - still there
delete cannot remove variables declared with var, let, or const. In sloppy mode it returns false; in strict mode it throws a SyntaxError if you try.
Arrays - creates a hole, not a removal
const arr = [1, 2, 3, 4];
delete arr[1];
console.log(arr); // [1, empty × 1, 3, 4] - a "hole"
console.log(arr[1]); // undefined
console.log(arr.length); // 4 - length UNCHANGED
This is rarely what you want. To truly remove an element, use splice:
arr.splice(1, 1); // removes 1 element at index 1, shifts remaining down
Memory - not a destructor
Setting properties to null/undefined or using delete does not directly free memory. JavaScript uses garbage collection - memory is reclaimed automatically when an object has no more references. delete helps by detaching a reference, but you don't manage memory explicitly.
Non-configurable Built-in Properties
Certain properties can't be deleted:
delete Math.PI; // false - non-configurable
delete undefined; // false (global property)
delete Object.prototype; // false
When delete Is Legitimate
- Removing a key from a cache/dictionary object when you're done with it.
- Stripping optional fields from a serialization object before sending.
- Removing a dynamically added property from an instance.
const cache = {};
cache["user:42"] = data;
// later:
delete cache["user:42"];
Why This Matters in Interviews
JS interviews use delete to probe understanding of object internals. Key points: it removes a property (not a variable), returns true even when the property doesn't exist, does not affect variables, leaves holes in arrays (use splice instead), and does not free memory directly. Non-configurable properties are undeletable. These distinctions - especially delete on arrays vs splice, and that variables are immune - are the classic traps.
Common Pitfalls
- Using
delete arr[i]expecting a clean removal - it leaves anundefinedhole and doesn't changelength; usesplice. - Expecting
delete variableto work - it doesn't remove declared variables. - Thinking
deletefrees memory immediately - GC handles that. - Forgetting
deletethrows in strict mode for non-configurable properties instead of silently failing.
References
- MDN
deleteoperator: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete