Skip to content
Tutorials Center
My learning

Topics

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.

JavaScript
delete object.property
delete object[expression]

Try it

JS Removing properties

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS Arrays and protected properties

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Syntax

JavaScript
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 keeps length. Use splice() to remove and shift, or filter() to build a new array.
  • Variables: declared variables (var, let, const, functions) are not deletable. In sloppy mode delete x for a variable returns false; 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 undefined or null may be better; for dynamic key sets use a Map.
  • Immutable style: to produce a copy without a key, use rest destructuring: const { role, ...rest } = user;.
  • Frozen and sealed objects reject deletion (Object.freeze() and Object.seal() make all properties non-configurable).
  • Optional chaining: delete a?.b is valid and returns true without doing anything when a is nullish.

Browser support

Supported in every browser and runtime.