Object.getOwnPropertyDescriptor()
You'll learn how to inspect the complete configuration of a single property, revealing all four attributes that control its behavior.
What You're Inspecting
You've already learned that properties have four descriptor attributes: value, writable, enumerable, and configurable. But how do you see what those settings actually are for a given property? That's where Object.getOwnPropertyDescriptor() comes in—it returns an object containing all these details.
The Syntax
const descriptor = Object.getOwnPropertyDescriptor(obj, propertyName);
It takes two arguments: the object you're examining and the property name (as a string). It returns a descriptor object or undefined if the property doesn't exist.
Seeing the Full Picture
const user = {
name: "Alice",
age: 30
};
const descriptor = Object.getOwnPropertyDescriptor(user, "name");
console.log(descriptor);
// {
// value: "Alice",
// writable: true,
// enumerable: true,
// configurable: true
// }
For a regular property created via an object literal, all flags default to true.
Why This Matters
This is your debugging tool when properties behave unexpectedly. If a property won't update, check if writable: false. If it's missing from loops, check enumerable: false.
Object.defineProperty(user, "id", {
value: 123,
writable: false
});
const idDescriptor = Object.getOwnPropertyDescriptor(user, "id");
console.log(idDescriptor.writable); // false — now you know why it won't change!
Key Takeaway: Object.getOwnPropertyDescriptor() reveals the complete configuration of a property, showing all four descriptor attributes that control how it behaves.