JavaScript Object.defineProperty() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Object

Definition

Object.defineProperty(obj, prop, descriptor) adds or changes one property on obj. The descriptor controls the value and the rules around it.

For a simple data property, set value, writable, enumerable, and configurable. The method returns the same object.

Syntax

Call Object.defineProperty() like this. Start with this line.

JavaScript
Object.defineProperty(obj, prop, descriptor)
CallWhat it does
Object.defineProperty(obj, prop, descriptor)Defines or updates one property on obj and returns obj.

Built into JavaScript. Call on Object — no import required.

Library declarations
JavaScript
Object.defineProperty(obj, prop, descriptor)
JavaScript
defineProperty(o: any, p: PropertyKey, attributes: PropertyDescriptor): any;

Parameters

ParameterDescription
objRequired. The object that will hold the property.
propRequired. The property name (string or Symbol).
descriptorRequired. A descriptor with fields such as value, writable, enumerable, and configurable.

Return Value

Return ValueDescription
objectThe same obj, after the property is defined.

JavaScript Examples

Example 1 — Basic Use

Define a normal writable, enumerable property on an empty object.

JavaScript
var book = {};
Object.defineProperty(book, "title", {
  value: "JS Tips",
  writable: true,
  enumerable: true,
  configurable: true
});

console.log(book.title);  // JS Tips
book.title = "More Tips";
console.log(book.title);  // More Tips
Try It Yourself

How It Works

  1. Define the field. title is added with value "JS Tips".
  2. Writable is true. Assignment is allowed, so the value can change.
  3. Read again. After the update, book.title is "More Tips".

Example 2 — Hidden Property

Set enumerable to false so the key stays out of Object.keys().

JavaScript
var box = {};
Object.defineProperty(box, "secret", {
  value: 42,
  writable: false,
  enumerable: false,
  configurable: false
});

console.log(box.secret);                          // 42
console.log(Object.keys(box).join("|") || "(none)"); // (none)
Try It Yourself

How It Works

  1. Still readable. You can read box.secret directly.
  2. Not in keys. enumerable: false keeps it out of Object.keys.
  3. Locked down. writable and configurable are false, so it is hard to change or remove.

Example 3 — Locked Value

A non-writable property keeps its original value in non-strict mode.

JavaScript
var box = {};
Object.defineProperty(box, "secret", {
  value: 42,
  writable: false,
  enumerable: false,
  configurable: false
});

box.secret = 100;
console.log(box.secret);  // 42
Try It Yourself

How It Works

  1. Try to change it. box.secret = 100 runs without throwing in non-strict mode.
  2. Value unchanged. Because writable is false, the value stays 42.
  3. Strict mode note. In strict mode the same assignment would throw a TypeError.

Browser & Runtime Support

Object.defineProperty() has been part of JavaScript since ES5 and is Baseline Widely available.

✓ Baseline · ES5

Object.defineProperty()

Supported in all modern browsers and Node.js. Internet Explorer 9+ supports it for plain objects.

99% Modern browser support
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
Object.defineProperty Excellent

Bottom line: Use Object.defineProperty() when you need descriptor control. Everyday fields can still use normal assignment.

Frequently Asked Questions

It defines or updates one property on an object using a descriptor, then returns that object.
Use defineProperty when you need flags like non-writable, non-enumerable, or non-configurable. For normal data, simple assignment is fine.
If enumerable is true, the property shows up in Object.keys() and for...in. If false, it is hidden from those lists.
The value cannot be changed. In non-strict mode the assignment is ignored. In strict mode it throws TypeError.
Only if configurable is true. If configurable is false, you generally cannot delete it or change its descriptor.

Next: entries()

Turn an object into [key, value] pairs.

entries() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful