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:
- target: This parameter deletes the property and it is the target object.
- propertyKey: This parameter is the name of the property which is to be deleted.
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.
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);
|
Outputundefined
[ 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.
javascript
console.log(Reflect.deleteProperty({}, 'geeks' ))
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" ));
|
Outputtrue
false
true
true
Supported Browsers:
The browsers supported by Reflect.deleteProperty() method are listed below:
- Google Chrome 49 and above
- Edge 12 and above
- Firefox 42 and above
- Opera 36 and above
- Safar 10 and above
We have a complete list of Javascript Reflects methods, to check those go through the JavaScript Reflect Reference article.