Skip to content
Related Articles

Related Articles

Improve Article
JavaScript | Reflect.deleteProperty() Method
  • Last Updated : 21 Mar, 2020

The 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 accept two parameters as mentioned avobe and described below:

  • target: This parameter delete the property and it is the target object.
  • propertyKey: This parameter is name of the property which is to be deleted.

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

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



Example 1:




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
Array [1, 2, 3, 4, 5]
Array [1, undefined, 3, 4, 5]
Array [1, undefined, undefined, 4, 5]

Example 2:




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

Output:

true false true true

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

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Opera
  • Safari



My Personal Notes arrow_drop_up
Recommended Articles
Page :