# Property Descriptors and Object.defineProperty

> What are property descriptors in JavaScript? How does Object.defineProperty work, and what do writable, enumerable and configurable control?

Canonical: https://www.internhack.in/learn/interview/javascript-interview/js-property-descriptors
Last updated: 2026-11-28

Difficulty: Advanced

## Question

What are property descriptors in JavaScript? How does Object.defineProperty work, and what do writable, enumerable and configurable control?

## Answer

Every property on a JavaScript object has a hidden descriptor that controls how it behaves: its value or getter/setter, and three boolean flags, `writable`, `enumerable` and `configurable`. `Object.defineProperty(obj, key, descriptor)` creates or changes a property with an exact descriptor, and `Object.getOwnPropertyDescriptor(obj, key)` reads one. This is how you make read-only, hidden or computed properties, and it is the machinery under `Object.freeze`, class getters and Vue 2's reactivity.

### Data descriptors vs accessor descriptors

A descriptor is one of two kinds, never both:

- **Data descriptor**: `value` and `writable`, plus `enumerable` and `configurable`.
- **Accessor descriptor**: `get` and/or `set` functions, plus `enumerable` and `configurable`.

Mixing them, for example `{ value: 1, get() {} }`, throws a `TypeError`.

### What each flag controls

| Flag | When false | Default via assignment | Default via defineProperty |
| --- | --- | --- | --- |
| `writable` | Value cannot be reassigned (silent fail in sloppy mode, `TypeError` in strict mode) | `true` | `false` |
| `enumerable` | Hidden from `for...in`, `Object.keys`, `JSON.stringify`, spread | `true` | `false` |
| `configurable` | Cannot be deleted, cannot switch between data and accessor, flags cannot be changed (except `writable` true to false) | `true` | `false` |

The defaults column is the classic trap: `Object.defineProperty(obj, 'x', { value: 1 })` creates a property that is read-only, hidden and permanent, because every omitted flag defaults to `false`.

### Object-level helpers built on descriptors

- `Object.preventExtensions(obj)`: no new properties can be added.
- `Object.seal(obj)`: preventExtensions plus every property becomes `configurable: false`. Values can still change.
- `Object.freeze(obj)`: seal plus every data property becomes `writable: false`.
- `Object.defineProperties` and `Object.getOwnPropertyDescriptors` work on many properties at once. `Object.create(proto, Object.getOwnPropertyDescriptors(src))` is a way to copy an object including its getters, which spread and `Object.assign` cannot do (they invoke getters and copy the values).

All three object helpers are shallow: freezing a config object does not freeze its nested objects.

### Where descriptors show up in real code

- **Built-ins**: `Array.prototype.map` is non-enumerable, which is why `for...in` over an array does not list methods. `Math.PI` is non-writable and non-configurable.
- **Classes**: methods defined in a class body are non-enumerable on the prototype; getters become accessor properties.
- **Vue 2 reactivity**: Vue 2 walked data objects and redefined every property as a getter/setter pair with `defineProperty`. That is why it could not detect newly added keys, and why Vue 3 moved to `Proxy`.
- **Libraries and SDKs**: hiding internal fields from serialisation, defining lazy computed values, or exposing read-only constants such as a payment gateway's API version.

### Gotchas

- Writing to a non-writable property fails silently outside strict mode, which hides bugs. ES modules and classes are always strict, so you get a `TypeError` there.
- A non-writable property on a prototype also blocks assignment of that key on objects that inherit from it (the "override mistake").
- Once `configurable` is `false`, you cannot make it configurable again.

**writable, enumerable, configurable. Toggle read-only, freeze or hidden properties and try each operation under Tweak.**

1. **writable: false**: Writes fail (TypeError in strict mode, silently ignored in sloppy mode).
2. **configurable: false**: The property can't be deleted or redefined.
3. **enumerable: false**: Hidden from Object.keys, for...in and JSON.stringify, but still readable.
4. **Read-only isn't undeletable**: writable: false alone still allows delete.

## Code examples

### Read-only and hidden properties

```
'use strict';
const payment = { orderId: 'ord_9F2', amountInPaise: 49900 };

Object.defineProperty(payment, 'gatewaySecret', {
  value: 'sk_test_123',
  // writable, enumerable, configurable all default to false
});

console.log(Object.keys(payment));
console.log(JSON.stringify(payment));
console.log(Object.getOwnPropertyDescriptor(payment, 'gatewaySecret'));

try {
  payment.gatewaySecret = 'changed';
} catch (e) {
  console.log(e.constructor.name);
}
try {
  delete payment.gatewaySecret;
} catch (e) {
  console.log(e.constructor.name);
}
```

Omitting the flags makes the property hidden from keys and JSON, read-only and undeletable. In strict mode both the write and the delete throw instead of failing silently.

### Accessor descriptor and freeze being shallow

```
const cart = { items: [{ price: 300 }, { price: 450 }] };

Object.defineProperty(cart, 'total', {
  get() { return this.items.reduce((sum, i) => sum + i.price, 0); },
  enumerable: true,
});
console.log(cart.total);
cart.items.push({ price: 250 });
console.log(cart.total); // recomputed on every read

const config = Object.freeze({ api: { version: 'v1' } });
config.api.version = 'v2'; // nested object is not frozen
console.log(config.api.version, Object.isFrozen(config), Object.isFrozen(config.api));
```

A getter computes the value on every access, so total stays in sync with items. Object.freeze only freezes the top level, so nested objects need a recursive deep freeze.

## Key points

- Every property has a descriptor: value/writable or get/set, plus enumerable and configurable.
- Flags default to true for normal assignment but false when omitted in Object.defineProperty.
- configurable: false prevents deletion and most descriptor changes, and cannot be undone.
- preventExtensions, seal and freeze are layered on descriptors and are all shallow.
- Vue 2 used defineProperty getters and setters for reactivity; Vue 3 switched to Proxy.

## Common follow-up questions

- How would you implement a deepFreeze function?
- What is the difference between Object.seal and Object.freeze?
- Why can Object.defineProperty not detect new properties being added?
- How do you copy an object including its getters and setters?
- What happens when you assign to a property that is non-writable on the prototype?

## Interview tips

- Mention the default-to-false trap with defineProperty; it shows you have actually used the API.
- Connect descriptors to things the interviewer knows: Object.freeze, class methods being non-enumerable, Vue 2 reactivity.
- Note the strict mode difference: silent failure vs TypeError.

## Concepts covered

Property Descriptor, Object.defineProperty, writable, enumerable, configurable, Accessor Property

Previous: [Memoization](https://www.internhack.in/learn/interview/javascript-interview/js-memoization) · Next: [Deep Cloning: structuredClone vs JSON](https://www.internhack.in/learn/interview/javascript-interview/js-structuredclone) · [All of JavaScript Interview](https://www.internhack.in/learn/interview/javascript-interview)

## More JavaScript Interview questions

- [JavaScript Design Patterns](https://www.internhack.in/learn/interview/javascript-interview/js-design-patterns)
- [Memory Leaks in JavaScript](https://www.internhack.in/learn/interview/javascript-interview/js-memory-leaks)
- [Custom Error Types and Error Handling](https://www.internhack.in/learn/interview/javascript-interview/js-error-types)
- [WebSockets and Real-time Communication](https://www.internhack.in/learn/interview/javascript-interview/js-websockets)
- [Advanced Regular Expressions](https://www.internhack.in/learn/interview/javascript-interview/js-regex-advanced)
- [JavaScript Performance Optimization](https://www.internhack.in/learn/interview/javascript-interview/js-performance-optimization)

## Related

- [Learn](https://www.internhack.in/learn)
- [Interview Prep](https://www.internhack.in/learn/interview)
- [JavaScript Interview](https://www.internhack.in/learn/interview/javascript-interview)
