Path: blob/main/files/en-us/web/javascript/reference/operators/delete/index.md
6520 views
------{{jsSidebar("Operators")}}
The delete operator removes a property from an object. If the property's value is an object and there are no more references to the object, the object held by that property is eventually released automatically.
{{EmbedInteractiveExample("pages/js/expressions-deleteoperator.html")}}
Syntax
Note: The syntax allows a wider range of expressions following the
deleteoperator, but only the above forms lead to meaningful behaviors.
Parameters
object: The name of an object, or an expression evaluating to an object.
property: The property to delete.
Return value
true for all cases except when the property is an own non-configurable property, in which case false is returned in non-strict mode.
Exceptions
{{jsxref("TypeError")}}
: Thrown in strict mode if the property is an own non-configurable property.
{{jsxref("ReferenceError")}}
: Thrown if
objectissuper.
Description
The delete operator has the same precedence as other unary operators like typeof. Therefore, it accepts any expression formed by higher-precedence operators. However, the following forms lead to early syntax errors in strict mode:
Because classes are automatically in strict mode, and private properties can only be legally referenced in class bodies, this means private properties can never be deleted. While delete identifier may work if identifier refers to a configurable property of the global object, you should avoid this form and prefix it with globalThis instead.
While other expressions are accepted, they don't lead to meaningful behaviors:
The delete operator removes a given property from an object. On successful deletion, it will return true, else false will be returned. Unlike what common belief suggests (perhaps due to other programming languages like delete in C++), the delete operator has nothing to do with directly freeing memory. Memory management is done indirectly via breaking references. See the memory management page for more details.
It is important to consider the following scenarios:
If the property which you are trying to delete does not exist,
deletewill not have any effect and will returntrue.deleteonly has an effect on own properties. If a property with the same name exists on the object's prototype chain, then after deletion, the object will use the property from the prototype chain.Non-configurable properties cannot be removed. This includes properties of built-in objects like {{jsxref("Math")}}, {{jsxref("Array")}}, {{jsxref("Object")}} and properties that are created as non-configurable with methods like {{jsxref("Object.defineProperty()")}}.
Deleting variables, including function parameters, never works.
delete variablewill throw a {{jsxref("SyntaxError")}} in strict mode, and will have no effect in non-strict mode.Any variable declared with {{jsxref("Statements/var", "var")}} cannot be deleted from the global scope or from a function's scope, because while they may be attached to the global object, they are not configurable.
Any variable declared with {{jsxref("Statements/let","let")}} or {{jsxref("Statements/const","const")}} cannot be deleted from the scope within which they were defined, because they are not attached to an object.
Examples
Using delete
Note: The following example uses non-strict-mode only features, like implicitly creating global variables and deleting identifiers, which are forbidden in strict mode.
delete and the prototype chain
In the following example, we delete an own property of an object while a property with the same name is available on the prototype chain:
Deleting array elements
When you delete an array element, the array length is not affected. This holds even if you delete the last element of the array.
When the delete operator removes an array element, that element is no longer in the array. In the following example, trees[3] is removed with delete.
This creates a sparse array with an empty slot. If you want an array element to exist but have an undefined value, use the undefined value instead of the delete operator. In the following example, trees[3] is assigned the value undefined, but the array element still exists:
If instead, you want to remove an array element by changing the contents of the array, use the {{jsxref("Array/splice", "splice()")}} method. In the following example, trees[3] is removed from the array completely using {{jsxref("Array/splice", "splice()")}}:
Deleting non-configurable properties
When a property is marked as non-configurable, delete won't have any effect, and will return false. In strict mode, this will raise a TypeError.
{{jsxref("Statements/var","var")}} creates non-configurable properties that cannot be deleted with the delete operator:
In strict mode, this would raise an exception.
Deleting global properties
If a global property is configurable (for example, via direct property assignment), it can be deleted, and subsequent references to them as global variables will produce a {{jsxref("ReferenceError")}}.
Specifications
{{Specifications}}
Browser compatibility
{{Compat}}
See also
{{jsxref("Reflect.deleteProperty()")}}
{{jsxref("Map.prototype.delete()")}}