Reflect.getOwnPropertyDescriptor() JavaScript

The JavaScript Reflect.getOwnPropertyDescriptor() method returns the descriptor of the property of an object. It is similar to Object.getOwnPropertyDescriptor().

Syntax:

Reflect.getOwnPropertyDescriptor(target, propertyKey)

Parameters:
target: It represents the object in which to look for the property.
propertyKey: It represents the name of the property whose descriptor has to be fetched.

Return:
It returns the descriptor of the specified property if it exists on the object otherwise returns undefined.

Note: It throws TypeError if the target is not an Object.

Example 1:

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

Example 2:

<!DOCTYPE html>
<html>
<body>
<script>
const object1 = {
property1: 542
};
document.write(Reflect.getOwnPropertyDescriptor(object1, 'property1').value);
document.write("</br>");
document.write(Reflect.getOwnPropertyDescriptor(object1, 'property2'));
document.write("</br>");
document.write(Reflect.getOwnPropertyDescriptor(object1, 'property1').writable);
</script>
</body>
</html>