contents   index   previous   next



delete operator

 

The delete operator deletes properties from objects and elements from arrays. Deleted properties and arrays are actually undefined. Any memory cleanup is handled by normal garbage collection.

 

The following fragment defines an array with three elements: 0, 1, and 2, and an object with three properties: four, five, and six. It then deletes the middle, that is, the second, element of the array and property of the object.

 

var a = ["one", "two", "three"];

var o = {four:444, five:555, six:666};

 

delete(a[1]);

delete(o.five);

 

There are several ways to eliminate the data in a property of an object or in an element of an array. The delete operator is the most complete way. Three other techniques use undefine(), undefined, and void, as illustrated next:

 

undefine(a[1]);

undefine(o.five);

 

a[1] = undefined;

o.five = undefined;

 

a[1] = void a[1];

o.five = void o.five;

 

These three techniques may be used with any variable, whereas the delete operator may be used only with properties of objects and elements of arrays. Generally, delete is the best to use with properties of objects and elements of arrays, thought in specific situations the other techniques might be preferable.

 

See global.undefine() and undefined for more information.

 


in operator