The Reflect.deleteProperty()
is used to delete the property of an object.
Reflect.deleteProperty(targetObj, propertyName)
targetObj
: The object in which the property is to be removed.
propertyName
: The property’s name to be removed from the target object.
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.
Remove a property from the object.
let obj = { a: 10, b: 20 };console.log("The object is :", obj);let isDeleted = Reflect.deleteProperty(obj, 'a') // trueconsole.log("isDeleted", isDeleted);console.log("The object is :", obj);
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.
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);
In the code above, we have created an object with property a
and frozen the object using 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.