Skip to main content

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

FlagWhat it doesDefault
writableAllows changing the property's valuefalse (if the property was created via defineProperty)
enumerableMakes the property visible during enumeration (for...in, Object.keys)false
configurableAllows deleting the property and changing its flagsfalse

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 enumerated

1. 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); // 25
  • writable: true -> the value can be changed
  • writable: 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); // nothing
  • enumerable: true -> the property is visible in for...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: id
  • configurable: true -> the property can be deleted or its flags changed
  • configurable: false -> it cannot be deleted, and cannot be changed for enumerable / 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

FlagPurposeExample
writableWhether the property's value can be changeduser.age = 30
enumerableWhether the property is visible during enumerationObject.keys(user)
configurableWhether the property can be deleted or redefineddelete user.prop

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.