JavaScript Reference ยท Operators
The delete operator
Reference for the JavaScript delete operator: removing properties, return values, non-configurable properties, arrays, strict mode errors and alternatives.
The delete operator removes a property from an object. It does not free memory directly and it cannot delete variables; it only removes the key (and its value) so that the property no longer exists on that object. Memory is reclaimed later by the garbage collector if nothing else refers to the value.
delete object.property delete object[expression]
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Syntax
delete obj.prop delete obj["prop"] delete obj?.prop
Return value: true if the property was deleted or did not exist; false if the property exists and is non-configurable (in sloppy mode). Deleting something that isn't a property reference (such as delete 1) returns true.
Exceptions:
| Situation | Result |
|---|---|
| Non-configurable own property, strict mode (including modules and classes) | TypeError |
delete variableName in strict mode |
SyntaxError |
delete super.x |
ReferenceError |
Deleting a private field (delete this.#x) |
SyntaxError |
delete null.x or delete undefined.x |
TypeError |
Usage notes
- Own properties only. Inherited properties cannot be deleted through a child; delete them on the object that owns them.
- Arrays get holes.
delete arr[i]leaves an empty slot and keepslength. Usesplice()to remove and shift, orfilter()to build a new array. - Variables: declared variables (
var,let,const, functions) are not deletable. In sloppy modedelete xfor a variable returnsfalse; in strict mode it is a syntax error. - Performance: deleting properties can make objects slower in some engines. When you only want to clear a value, setting it to
undefinedornullmay be better; for dynamic key sets use aMap. - Immutable style: to produce a copy without a key, use rest destructuring:
const { role, ...rest } = user;. - Frozen and sealed objects reject deletion (
Object.freeze()andObject.seal()make all properties non-configurable). - Optional chaining:
delete a?.bis valid and returnstruewithout doing anything whenais nullish.
Browser support
Supported in every browser and runtime.