The delete operator

suggest change

The delete operator deletes a property from an object.

Syntax:

delete object.property

delete object['property']

Returns:

If deletion is successful, or the property did not exist:

If the property to be deleted is an own non-configurable property (can’t be deleted):

Description

The delete operator does not directly free memory. It can indirectly free memory if the operation means all references to the property are gone.

delete works on an object’s properties. If a property with the same name exists on the object’s prototype chain, the property will be inherited from the prototype.

delete does not work on variables or function names.

Examples:

// Deleting a property
foo = 1;              // a global variable is a property of `window`: `window.foo`
delete foo;           // true
console.log(foo);     // Uncaught ReferenceError: foo is not defined

// Deleting a variable
var foo = 1;
delete foo;           // false
console.log(foo);     // 1 (Not deleted)

// Deleting a function
function foo(){ };
delete foo;           // false
console.log(foo);     // function foo(){ } (Not deleted)

// Deleting a property
var foo = { bar: "42" };
delete foo.bar;       // true
console.log(foo);     // Object { } (Deleted bar)

// Deleting a property that does not exist
var foo = { };
delete foo.bar;       // true
console.log(foo);     // Object { } (No errors, nothing deleted)

// Deleting a non-configurable property of a predefined object
delete Math.PI;       // false  ()
console.log(Math.PI); // 3.141592653589793 (Not deleted)

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Unary Operators:
* Syntax
* The delete operator

Table Of Contents
11 Arrays
12 Objects
14 Classes
16 Map
17 Set
21 Unary Operators
24 Loops
27 Date
29 Scope
30 AJAX
35 Cookies
41 JSON
44 Fetch
45 Modules
46 Screen
64 Console
68 Symbols
73 Modals
76 Events
86 Proxy
89 WeakMap
90 WeakSet
102 Tilde