JavaScript Property Descriptors and Attribute Flags
Quick answer
A property descriptor describes a data property or accessor property and the attributes that control its behavior.
How it works
Object.getOwnPropertyDescriptor returns an ordinary descriptor object or undefined for a missing own property. Data descriptors have value and writable; accessor descriptors have get and set. Both can have enumerable and configurable. Writable controls ordinary data writes, enumerable controls inclusion in selected enumeration APIs, configurable controls deletion and most redefinitions. A nonconfigurable writable data property can still change value and become nonwritable.
const o = {n: 3}; const d = Object.getOwnPropertyDescriptor(o,"n");console.log(d.value,d.writable,d.enumerable,d.configurable);Expected output in Node.js:
3 true true trueAn ordinary literal data property has all three flags true.
const o = {n: 3}; const d = Object.getOwnPropertyDescriptor(o,"n");d.value = 9; console.log(o.n, Object.getOwnPropertyDescriptor(o,"missing"));Expected output in Node.js:
3 undefinedThe descriptor is a snapshot of attributes; the missing own key has no descriptor.
Common mistakes
Modifying the returned descriptor object does not reconfigure the original property.
Practice
Inspect descriptors of two literal properties and one missing key.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
JavaScript Property Descriptors and Attribute Flags — Knowledge Check
Apply the taught concept and predict the output.
Which flag controls ordinary data-property assignment?
Which descriptor kind has get and set fields?
What does getOwnPropertyDescriptor return for a missing own key?
What is the exact console output in JavaScript Property Descriptors and Attribute Flags example 1?
const o = {n: 3}; const d = Object.getOwnPropertyDescriptor(o,"n");console.log(d.value,d.writable,d.enumerable,d.configurable);What is the exact console output in JavaScript Property Descriptors and Attribute Flags example 2?
const o = {n: 3}; const d = Object.getOwnPropertyDescriptor(o,"n");d.value = 9; console.log(o.n, Object.getOwnPropertyDescriptor(o,"missing"));