getOwnPropertyDescriptor() - JavaScript Tutorial

getOwnPropertyDescriptor()

BLUF: This tutorial on getOwnPropertyDescriptor() provides an in-depth look at JavaScript's core features. It includes practical examples and code snippets to help you master modern JS development.
Key Discovery: getOwnPropertyDescriptor()

Understanding getOwnPropertyDescriptor() is crucial for building dynamic, interactive web applications. Explore the examples below to see it in action.

The static method Reflect.getOwnPropertyDescriptor serves the purpose of obtaining the descriptor for a specific property of an object. This functionality is identical to that of the Object.getOwnPropertyDescriptor method.

Syntax:

Example

Reflect.getOwnPropertyDescriptor (obj,  Key)

Parameters:

Obj : This refers to the object that serves as the target for searching for the property.

Key: This refers to the identifier of the property for which an individual property descriptor is to be obtained.

Return value:

If the specified property is present in the target object, it will return the corresponding property descriptor object. If the property does not exist, it will return undefined.

Exceptions:

A TypeError, if the target is not an Object.

Browser Support:

Chrome 49
Edge 12
Firefox 42
Opera 36

Example 1

Example

const object1 = {
  property1: 22    };
console.log(Reflect.getOwnPropertyDescriptor(object1, 'property2'));
console.log(Reflect.getOwnPropertyDescriptor(object1, 'property1').writable);

Output:

Output

undefined
 true

Example 2

Example

const object1 = {
  property1: 234    };
const hh = {p:4};
console.log(Reflect.getOwnPropertyDescriptor(object1, 'property1').value);

console.log(Reflect.getOwnPropertyDescriptor(object1, 'property2'));

console.log(Reflect.getOwnPropertyDescriptor(object1, 'property1').writable);

console.log (
 Reflect.getOwnPropertyDescriptor ( hh , "yyy" ) === undefined
);

Output:

Output

234
Undefined
true
true

Example 3

Example

const object1 = {
  property1: 42
};
console.log(Reflect.getOwnPropertyDescriptor(object1, 'property1').value);

console.log(Reflect.getOwnPropertyDescriptor(object1, 'property2'));

console.log(Reflect.getOwnPropertyDescriptor(object1, 'property1').enumerable);

Output:

Output

42
undefined
true

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience