Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

javascript
property
delete
communitycreator

What is Reflect.deleteProperty() in JavaScript?

Programming Bytes

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The Reflect.deleteProperty() is used to delete the property of an object.

Syntax


Reflect.deleteProperty(targetObj, propertyName)

Arguments

  • targetObj: The object in which the property is to be removed.

  • propertyName: The property’s name to be removed from the target object.

Return value

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.

Code

Example 1

Remove a property from the object.

let obj = { a: 10, b: 20 };
console.log("The object is :", obj);
let isDeleted = Reflect.deleteProperty(obj, 'a') // true
console.log("isDeleted", isDeleted);
console.log("The object is :", obj);

Explanation

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.

Example 2

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

Explanation

In the code above, we have created an object with property a and frozen the object using Object.freezeFreezing the object will prevent the object from adding, deleting, and modifying the property of the object.. Then we tried to delete the property 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.

RELATED TAGS

javascript
property
delete
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring