Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

JavaScript · Function

Object.defineProperty

Defines or modifies an object's own property with a descriptor, controlling its value, accessors, and property flags. On success, it returns the original object.

How it works

Object.defineProperty() adds or updates an own property using a descriptor. The descriptor can control a data property's value, writability, enumerability, and configurability, or define accessor functions with get and set; omitted attributes default to false.

Syntax

Object.defineProperty(obj, prop, descriptor)

Parameters

NameTypeDescription
objObjectThe object on which to define the property.
propstring | SymbolA string or Symbol specifying the property key to define or modify.
descriptorObjectThe descriptor specifying the property's data or accessor attributes.

Returns

The object passed as obj, with the specified property added or modified.

Key points

  • It can create a property or change one already present, and its successful result is the original object.
  • The property key may be either a string or a Symbol.
  • The descriptor controls the property's value or accessors and its behavior flags.
  • Compared with ordinary assignment, it gives explicit control over enumerability, writability, and configurability.
  • Its definition semantics bypass an existing setter on the target property.

Common mistakes

  • If descriptor flags are omitted, the new property is read-only, hidden from ordinary enumeration, and unable to be reconfigured.
  • Assignments to a non-writable property fail silently in non-strict code but throw in strict mode.
  • Redefining an existing property can fail depending on its current non-configurable descriptor.

Example: Define a controlled property

const user = {};

Object.defineProperty(user, "id", {
  value: 42,
  writable: false,
  enumerable: true,
  configurable: true,
});

console.log(user.id); // 42
console.log(Object.keys(user)); // ["id"]

Defines an own data property whose value can be read and enumerated but not reassigned.

When to use it

Create a non-writable property

Expose a read-only public value while preventing reassignment.

Control property enumeration

Hide implementation details from Object.keys() and for...in.

Define accessor properties

Compute values dynamically or validate assignments.

Precisely modify object structure

Add or revise properties with explicit descriptor behavior.

Interview questions

QWhat does Object.defineProperty() do, and what does it return?

It defines or modifies an own property on the supplied object and returns that same object. The property behavior comes from the descriptor argument.

Follow-up: How does it differ from Reflect.defineProperty()? Reflect.defineProperty() returns a Boolean success status, whereas Object.defineProperty() returns the object on success and throws a TypeError on failure.

QHow do data and accessor descriptors differ?

A data descriptor uses value and writable; an accessor descriptor uses get and set. A descriptor must not combine data and accessor fields.

Follow-up: What happens when a descriptor omits configurable or enumerable? Those attributes default to false for a property created by this method.

QDoes Object.defineProperty() invoke an existing setter?

The method defines a property through the object's [[DefineOwnProperty]] internal method rather than [[Set]]. Consequently, it does not invoke a setter merely because the property already exists.

Follow-up: Why might a proxy still observe the operation? A proxy can intercept the corresponding operation through its handler.defineProperty() trap.

  • Object.defineProperties()
  • Object.getOwnPropertyDescriptor()
  • Reflect.defineProperty()
  • Proxy
  • handler.defineProperty()

Source: Object.defineProperty() - JavaScript | MDN. Checked 2026-10-15.