Skip to main content

What does Object.defineProperty() do?

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);
ArgumentDescription
objThe object in which the property is created or changed
propNameThe property name (a string or a Symbol)
descriptorA 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

AttributeDefaultWhat it does
valueundefinedThe property's value
writablefalseWhether the value can be changed
enumerablefalseWhether the property is visible when iterated
configurablefalseWhether the descriptor can be deleted / changed
getundefinedThe getter function
setundefinedThe setter function

Short Answer

Interview ready
Premium

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