What is Reflect.deleteProperty() in JavaScript?

The Reflect.deleteProperty() is used to delete the property of an object.

Syntax


Reflect.deleteProperty(targetObj, propertyName)

Arguments

  • targetObj: The object in which the property is to be removed.

  • propertyName: The property’s name to be removed from the target object.

Return value

This method returns true if the property is successfully removed from the target object. This means if the passed property name is not present in the object, then true is returned.

Code

Example 1

Remove a property from the object.

let obj = { a: 10, b: 20 };
console.log("The object is :", obj);
let isDeleted = Reflect.deleteProperty(obj, 'a') // true
console.log("isDeleted", isDeleted);
console.log("The object is :", obj);

Explanation

In the code above, we have created an object with two properties, a and b. Then we called the deletePoperty method to delete the a property. This will remove the property a from the object.

Example 2

Try to delete the frozen property.

let obj = {a:10};
console.log("The object is", obj);
Object.freeze(obj);
console.log("isDeleted", Reflect.deleteProperty(obj, 'a'));
console.log("The object is", obj);

Explanation

In the code above, we have created an object with property a and frozen the object using Object.freezeFreezing the object will prevent the object from adding, deleting, and modifying the property of the object.. Then we tried to delete the property a using the following.


 Reflect.deleteProperty(obj, 'a');

This call will return false because the properties of the object cannot be added, modified, or removed.

Free Resources