The writable, enumerable, configurable flags?
The writable, enumerable and configurable flags are three service parameters (flags)
in a property descriptor
that control the behavior of object properties in JavaScript.
These flags are not visible during regular use, but they determine whether a property can be changed, deleted or enumerated.
Summary
| Flag | What it does | Default |
|---|---|---|
writable | Allows changing the property's value | false (if the property was created via defineProperty) |
enumerable | Makes the property visible during enumeration (for...in, Object.keys) | false |
configurable | Allows deleting the property and changing its flags | false |
Example for all three
javascript
const user = {};
Object.defineProperty(user, 'name', {
value: 'Tim',
writable: false, // cannot be changed
enumerable: false, // does not appear in loops
configurable: false // cannot be deleted or have its flags changed
});
console.log(user.name); // "Tim"
user.name = 'Oleh'; // will not change
delete user.name; // will not be deleted
console.log(Object.keys(user)); // [] - not enumerated1. writable - "can the value be changed"
javascript
const user = {};
Object.defineProperty(user, 'age', {
value: 25,
writable: false
});
user.age = 30; // has no effect
console.log(user.age); // 25writable: true-> the value can be changedwritable: false-> the property is read-only
2. enumerable - "should the property show up during enumeration"
javascript
const user = {};
Object.defineProperty(user, 'secret', {
value: '12345',
enumerable: false
});
console.log(user.secret); // accessible directly
console.log(Object.keys(user)); // [] (not visible)
for (const key in user) console.log(key); // nothingenumerable: true-> the property is visible infor...in,Object.keys(),JSON.stringify()enumerable: false-> a "hidden" property
A real-world example: built-in methods (toString, valueOf) have enumerable: false
so they do not get in the way during enumeration.
3. configurable - "can it be deleted or have its flags changed"
javascript
const user = {};
Object.defineProperty(user, 'id', {
value: 10,
configurable: false
});
delete user.id; // will not be deleted
console.log(user.id); // 10
// Try to change writable:
Object.defineProperty(user, 'id', { writable: true });
// TypeError: Cannot redefine property: idconfigurable: true-> the property can be deleted or its flags changedconfigurable: false-> it cannot be deleted, and cannot be changed forenumerable/configurable/writable: false -> true
The only thing that can be changed is
writable: true -> false, but not the other way around.
How to get the flag values
javascript
const obj = { name: 'Tim' };
const descriptor = Object.getOwnPropertyDescriptor(obj, 'name');
console.log(descriptor);Outputs:
javascript
{
value: 'Tim',
writable: true,
enumerable: true,
configurable: true
}How to set flags
javascript
Object.defineProperty(obj, 'prop', {
value: 42,
writable: false,
enumerable: true,
configurable: false
});Summary
| Flag | Purpose | Example |
|---|---|---|
writable | Whether the property's value can be changed | user.age = 30 |
enumerable | Whether the property is visible during enumeration | Object.keys(user) |
configurable | Whether the property can be deleted or redefined | delete user.prop |
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.