Intermediate JavaScript Interview QuestionsIntermediateConcept
JavaScript · Question 61
What are JavaScript property descriptors, and what do writable, enumerable, and configurable control?
Direct answer
Property descriptors define how an own property behaves: data properties can control value and writability, accessors use get/set functions, and enumerable/configurable determine visibility in common enumeration and whether the property can be reconfigured or deleted.
Use Object.getOwnPropertyDescriptor(obj, key) to inspect a property and Object.defineProperty() to define one with explicit descriptor flags.
- writable — for data properties, controls whether assignment can change the stored value.
- enumerable — affects whether the property appears in operations such as
Object.keys()and normal own-property enumeration. - configurable — controls deletion and most future descriptor reconfiguration.
- get/set — accessor properties compute reads/writes rather than storing a normal
valuefield.
A subtle interview detail: properties created with ordinary assignment are normally writable, enumerable, and configurable, whereas flags omitted in a new descriptor passed to Object.defineProperty() default to false.