Open In App

JavaScript Reflect deleteProperty() Method

JavaScript Reflect.deleteProperty() method in JavaScript is used to delete a property on an object. It returns a Boolean value which indicates whether the property was successfully deleted.

Syntax:



Reflect.deleteProperty( target, propertyKey )

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.



Exceptions: A TypeError is an exception given as the result when the target is not a constructor.

Example 1: In this example, we will delete a property of an object using Reflect.deleteProperty() method in Javascript.




const object1 = {
    property1: 76
};
 
Reflect.deleteProperty(object1, 'property1');
 
console.log(object1.property1);
 
const array1 = [1, 2, 3, 4, 5];
Reflect.deleteProperty(array1, '12');
console.log(array1);
 
Reflect.deleteProperty(array1, '1');
console.log(array1);
 
Reflect.deleteProperty(array1, '2');
console.log(array1);

Output
undefined
[ 1, 2, 3, 4, 5 ]
[ 1, <1 empty item>, 3, 4, 5 ]
[ 1, <2 empty items>, 4, 5 ]

Example 2: In this example, we will delete a property of an object using Reflect.deleteProperty() method in Javascript.




// Returns true if no such property exists
console.log(Reflect.deleteProperty({}, 'geeks'))
 
// Returns false if a property is unconfigurable
console.log(Reflect.deleteProperty(
    Object.freeze({ geeks: 1 }), 'geeks'))
 
const obj = { val1: 22, val2: 434, val3: 42 };
const obj1 = { val: 5 };
console.log(Reflect.deleteProperty(obj, "val1"));
console.log(Reflect.deleteProperty(obj, "val2"));

Output
true
false
true
true

Supported Browsers:

The browsers supported by Reflect.deleteProperty() method are listed below:

We have a complete list of Javascript Reflects methods, to check those go through the JavaScript Reflect Reference article.


Article Tags :