The delete operator
suggest changeThe 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:
true
If the property to be deleted is an own non-configurable property (can’t be deleted):
falsein non-strict mode.- Throws an error in strict mode
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