Your Search Results

    delete Redirect 1

    Summary

    The delete operator removes a property from an object.

    Syntax

    delete expression 

    where expression should evaluate to a property reference, e.g.:

    delete object.property
    delete object['property']
    

    Parameters

    object
    The name of an object, or an expression evaluating to an object.
    property
    The property to delete.

    Returns

    Throws in strict mode if the property is an own non-configurable property (returns false in non-strict). Returns true in all other cases.

    Description

    Unlike what common belief suggests, the delete operator has nothing to do with directly freeing memory (it only does indirectly via breaking references. See the memory management page for more details).

    If the delete operator succeeds, it removes the property from the object entirely. However, if a property with the same name exists on the object's prototype chain, the object will inherit that property from the prototype.

    delete is only effective on an object's properties. It has no effect on variable or function names.
    While sometimes mis-characterized as global variables, assignments that don't specify an object (e.g. x = 5) are actually property assignments on the global object.

    delete can't remove certain properties of predefined objects (like Object, Array, Math etc). These are described in ECMAScript 5 and later as non-configurable.

    Examples

    x = 42;         // creates the property x on the global object
    var y = 43;     // declares a new variable, y
    myobj = {
      h: 4,
      k: 5
    };
    
    // x is a property of the global object and can be deleted
    delete x;       // returns true
    
    // delete doesn't affect variable names                
    delete y;       // returns false 
    
    // delete doesn't affect certain predefined properties
    delete Math.PI; // returns false 
    
    // user-defined properties can be deleted
    delete myobj.h; // returns true 
    
    // myobj is a property of the global object, not a variable,
    // so it can be deleted
    delete myobj;   // returns true

    If the object inherits a property from a prototype, and doesn't have the property itself, the property can't be deleted by referencing the object. You can, however, delete it directly on the prototype.

    function Foo(){}
    Foo.prototype.bar = 42;
    var foo = new Foo();
    
    // returns true, but with no effect, 
    // since bar is an inherited property
    delete foo.bar;           
    
    // logs 42, property still inherited
    console.log(foo.bar);
    
    // deletes property on prototype
    delete Foo.prototype.bar; 
    
    // logs "undefined", property no longer inherited
    console.log(foo.bar);           

    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.

    var trees = ["redwood","bay","cedar","oak","maple"];
    delete trees[3];
    if (3 in trees) {
        // this does not get executed
    }

    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:

    var trees = ["redwood","bay","cedar","oak","maple"];
    trees[3] = undefined;
    if (3 in trees) {
        // this gets executed
    }

    Specifications

    Specification Status Comment
    ECMAScript 1st Edition. Standard Initial definition. Implemented in JavaScript 1.2
    ECMAScript 5.1 (ECMA-262)
    The definition of 'The delete Operator' in that specification.
    Standard  
    ECMAScript 6 (ECMA-262)
    The definition of 'The delete Operator' in that specification.
    Draft  

    Browser compatibility

    Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
    Basic support (Yes) (Yes) (Yes) (Yes) (Yes)
    Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
    Basic support (Yes) (Yes) (Yes) (Yes) (Yes) (Yes)

    Cross-browser issues

    Although ECMAScript makes iteration order of objects implementation-dependent, it may appear that all major browsers support an iteration order based on the earliest added property coming first (at least for properties not on the prototype). However, in the case of Internet Explorer, when one uses delete on a property, some confusing behavior results, preventing other browsers from using simple objects like object literals as ordered associative arrays. In Explorer, while the property value is indeed set to undefined, if one later adds back a property with the same name, the property will be iterated in its old position--not at the end of the iteration sequence as one might expect after having deleted the property and then added it back.

    So if you want to simulate an ordered associative array in a cross-browser environment, you are forced to either use two separate arrays (one for the keys and the other for the values), or build an array of single-property objects, etc.

    See also

    Document Tags and Contributors

    Contributors to this page: Sheppy
    Last updated by: Sheppy,
    Hide Sidebar