Suggest an editImprove this articleRefine the answer for “What does Object.defineProperty() do?”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)**`Object.defineProperty()`** is a powerful built-in JavaScript method that lets you create or modify object properties with fine-grained control - for example, making them read-only, hidden, computed, and so on. **Key point:** the property's behavior is set by a descriptor with the `value`, `writable`, `enumerable`, and `configurable` attributes, and it can also contain `get`/`set` for computed properties.Shown above the full answer for quick recall.Answer (EN)Image`Object.defineProperty()` is a **powerful built-in JavaScript method** that lets you **create or modify object properties with fine-grained control** - for example, making them **read-only, hidden, computed**, and so on. --- ## Syntax ```javascript Object.defineProperty(obj, propName, descriptor); ``` | Argument | Description | |---|---| | `obj` | The object in which the property is created or changed | | `propName` | The property name (a string or a `Symbol`) | | `descriptor` | A descriptor object - specifies **how the property behaves** | --- ## Example - a regular property ```javascript const user = {}; Object.defineProperty(user, 'name', { value: 'Tim', writable: true, // can be changed enumerable: true, // visible when iterated (for...in / Object.keys) configurable: true // can be deleted or changed }); console.log(user.name); // "Tim" ``` This is the same as if you wrote: ```javascript const user = { name: 'Tim' }; ``` But with `defineProperty` you can **precisely control the property's behavior**. --- ## 1. `writable` - whether it can be changed ```javascript const user = {}; Object.defineProperty(user, 'name', { value: 'Tim', writable: false }); user.name = 'Oleh'; // will not change console.log(user.name); // "Tim" ``` `writable: false` makes the property **read-only**. --- ## 2. `enumerable` - whether the property is visible when iterated ```javascript const user = {}; Object.defineProperty(user, 'secret', { value: '12345', enumerable: false }); console.log(Object.keys(user)); // [] not visible console.log(user.secret); // can be read directly ``` Useful for "internal" data. --- ## 3. `configurable` - whether it can be deleted or redefined ```javascript const user = {}; Object.defineProperty(user, 'name', { value: 'Tim', configurable: false }); delete user.name; // will not work Object.defineProperty(user, 'name', { value: 'Oleh' }); // error ``` `configurable: false` makes the property **permanently "locked"**. --- ## 4. Getter and setter via a descriptor `Object.defineProperty()` can be used to create **computed properties**: ```javascript const user = { firstName: 'Tim', lastName: 'Cooper' }; Object.defineProperty(user, 'fullName', { get() { return `${this.firstName} ${this.lastName}`; }, set(value) { const [first, last] = value.split(' '); this.firstName = first; this.lastName = last; } }); console.log(user.fullName); // "Tim Cooper" user.fullName = 'Oleh Shevchenko'; console.log(user.firstName); // "Oleh" ``` If `get` or `set` are specified, `value` and `writable` cannot be used. --- ## 5. Inspect the current property settings ```javascript console.log(Object.getOwnPropertyDescriptor(user, 'fullName')); ``` Example output: ```javascript { get: [Function: get], set: [Function: set], enumerable: false, configurable: false } ``` --- ## 6. Create several properties at once ```javascript Object.defineProperties(user, { age: { value: 25, writable: true }, city: { value: 'Kyiv', enumerable: true } }); ``` `defineProperties()` is analogous, but for a group of properties. --- ## Summary | Attribute | Default | What it does | |---|---|---| | `value` | `undefined` | The property's value | | `writable` | `false` | Whether the value can be changed | | `enumerable` | `false` | Whether the property is visible when iterated | | `configurable` | `false` | Whether the descriptor can be deleted / changed | | `get` | `undefined` | The getter function | | `set` | `undefined` | The setter function |For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.