Saturday, November 16, 2024
Google search engine
HomeLanguagesJavascriptJavaScript Reflect deleteProperty() Method

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:

  • 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);


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.

javascript




// Returns true if no such property exists
console.log(Reflect.deleteProperty({}, 'neveropen'))
 
// Returns false if a property is unconfigurable
console.log(Reflect.deleteProperty(
    Object.freeze({ neveropen: 1 }), 'neveropen'))
 
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:

  • 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.

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments