Reflect.defineProperty() JavaScript

The JavaScript Reflect.defineProperty() adds or modifies a property of an object. It is the same as Object.defineProperty() but it returns a Boolean.

Syntax:

Reflect.defineProperty(target, propertyKey, attributes)

Parameters:
target: It represents the object that has to be invoked.
propertyKey: It represents the name of the property to be defined or modified.
attributes: It represents attributes for the property being defined or modified.

Return:
It returns true if the new property is successfully added or the existing property is modified successfully otherwise, it returns false.

Example 1:

<!DOCTYPE html>
<html>
<body>
<script>
const obj = {};
(Reflect.defineProperty(obj, 'prop', {value: 100}))
document.write(obj.prop);
</script>
</body>
</html>

Example 2:

<!DOCTYPE html>
<html>
<body>
<script>
const object1 = {};
if (Reflect.defineProperty(object1, 'property1', {value: 142})) {
document.write('property1 created successfully');
} else {
document.write('Error in creating property1');
}
document.write("</br>");
document.write(object1.property1);
</script>
</body>
</html>