A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/javascript/javascript-handler-deleteproperty-method/ below:

JavaScript Handler deleteProperty() Method - GeeksforGeeks

JavaScript Handler deleteProperty() Method

Last Updated : 12 Jul, 2025

JavaScript handler.deleteProperty() method in JavaScript is a trap for the delete operator. This method returns the boolean value if the delete was successful.

Syntax: 

const p = new Proxy(target, {
      deleteProperty: function(target, property) {
  }
});

Parameters: This method accepts two parameters as mentioned above and described below: 

Return value: This method returns a Boolean value that indicates whether the property was successfully deleted.

Below examples illustrate the handler.deleteProperty() method in JavaScript:

Example 1: In this example, we will trap a delete operator using the handler.deleteProperty() method in JavaScript.
 

javascript
const monster1 = {
    Color: 'Green'
};

const handler1 = {
    deleteProperty(target, prop) {
        if (prop in target) {
            delete target[prop];
            console.log(`${prop} is property which is removed`);
        }
    }
};

console.log(monster1.Color);

const proxy1 = new Proxy(monster1, handler1);
delete proxy1.Color;

console.log(monster1.Color);

let f = { bar: 'baz' }
console.log('bar' in f)

delete f.bar
console.log('bar' in f) 

Output: 

"Green"
"Color is property which is removed"
undefined
true
false

Example 2: In this example, we will trap a delete operator using the handler.deleteProperty() method in JavaScript.

javascript
const obj = new Proxy({}, {
    deleteProperty: function (target, prop) {
        if (prop in target) {
            delete target[prop]
            console.log(prop + ' property is removed.')
            return true
        }
        else {
            console.log(prop + ' property is not removed.')
            return false
        }
    }
})

let result

obj.prop1 = 10
console.log('prop1' in obj)

result = delete obj.prop1
console.log(result)
console.log('prop1' in obj)

result = delete obj.prop1
console.log(result)

Output: 

true
"prop1 property is removed."
true
false
"prop1 property is not removed."
false

Supported Browsers: The browsers supported by the handler.deleteProperty() method are listed below: 

We have a complete list of Javascript Proxy/handler methods, to check those go through the Javascript Proxy/handler Reference article.



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4