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:

In a non-strict script, an assignment to an undeclared name creates a configurable global property, and delete can remove it. Strict mode rejects delete of an unqualified name as a syntax error, and deleting a non-configurable property such as Math.PI throws.

foo = 1;              // configurable property of the global object
delete foo;           // true
console.log(foo);     // ReferenceError: foo is not defined
var foo = 1;
delete foo;           // false
console.log(foo);     // 1
function foo(){ }
delete foo;           // false
console.log(foo);     // function foo(){ }
var foo = { bar: "42" };
delete foo.bar;       // true
console.log(foo);     // {}
var foo = { };
delete foo.bar;       // true. An own property was not there to remove.
console.log(foo);     // {}
delete Math.PI;       // false in non-strict code
console.log(Math.PI); // 3.141592653589793

Feedback about page:

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



Table Of Contents