getOwnPropertyDescriptor() method - JavaScript Tutorial

getOwnPropertyDescriptor() method

BLUF: This tutorial on getOwnPropertyDescriptor() method 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() method

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

The Object.getOwnPropertyDescriptor function enables users to retrieve comprehensive details regarding a property and provides a property descriptor for an own property (meaning one that is directly located on an object rather than being inherited from the object's prototype chain) of a specified object.

Syntax:

Example

bject.getOwnPropertyDescriptor(obj, prop)

Parameter

obj: This refers to the object in which the property is to be searched for.

Prop: This term refers to the designation of the property for which the description is intended to be obtained.

Return value:

It provides a property descriptor for the specified property if that property is present on the object.

Browser Support:

Chrome 4
Edge Yes
Firefox 12
Opera 4

Example 1

Example

const object1 = {
  property1: 42
}
const object2 = {
  property2: 34
}
const descriptor1 = Object.getOwnPropertyDescriptor(object1, 'property1');
const descriptor2 = Object.getOwnPropertyDescriptor(object2, 'property2');
console.log(descriptor1.enumerable);
console.log(descriptor2.enumerable);
console.log(descriptor1.value);
console.log(descriptor2.value);

Output:

Output

true
true
42
34

Example 2

Example

const object1 = {
  property1: 42
}
const descriptor1 = Object.getOwnPropertyDescriptor(object1, 'property1');
console.log(descriptor1.configurable);
console.log(descriptor1.enumerable);
console.log(descriptor1.value);

Output:

Output

true
true
42

Example 3

Example

const object1 = {
  property1: 56
}
const descriptor1 = Object.getOwnPropertyDescriptor(object1, 'property1');
console.log(descriptor1.writable);
console.log(descriptor1.value);

Output:

Output

true
56

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