Object.getOwnPropertyDescriptor
What you'll learn: How to inspect the exact configuration of any property on an object.
Reading a Property's Descriptor
You've already learned that every property has a hidden descriptor object that controls whether it's writable, enumerable, or configurable. But how do you read that descriptor after a property exists?
Object.getOwnPropertyDescriptor(obj, propName) retrieves the descriptor object for a specific property. It returns an object with value, writable, enumerable, and configurable keys (or get/set for accessor properties).
Think of it like reading a product's specification sheet — instead of just seeing "the property exists," you see all its configuration details.
Syntax and Usage
const book = {
title: "JavaScript Guide"
};
const descriptor = Object.getOwnPropertyDescriptor(book, "title");
console.log(descriptor);
// {
// value: "JavaScript Guide",
// writable: true,
// enumerable: true,
// configurable: true
// }
Checking Locked Properties
This method is especially useful after using Object.defineProperty:
const user = {};
Object.defineProperty(user, "id", {
value: 42,
writable: false
});
const idDescriptor = Object.getOwnPropertyDescriptor(user, "id");
console.log(idDescriptor.writable); // false
console.log(idDescriptor.value); // 42
Returns undefined for Missing Properties
If the property doesn't exist on that specific object, you get undefined:
const obj = {};
console.log(Object.getOwnPropertyDescriptor(obj, "missing")); // undefined
Key Takeaway: Object.getOwnPropertyDescriptor lets you inspect a property's descriptor object, revealing whether it's writable, enumerable, and configurable — essential for understanding property behavior.