JavaScript Object.defineProperty() Method

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.
Object.defineProperty(obj, prop, descriptor)
| Call | What 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
Object.defineProperty(obj, prop, descriptor)
defineProperty(o: any, p: PropertyKey, attributes: PropertyDescriptor): any;
Parameters
| Parameter | Description |
|---|
| obj | Required. The object that will hold the property. |
| prop | Required. The property name (string or Symbol). |
| descriptor | Required. A descriptor with fields such as value, writable, enumerable, and configurable. |
Return Value
| Return Value | Description |
|---|
| object | The same obj, after the property is defined. |
JavaScript Examples
Example 1 — Basic Use
Define a normal writable, enumerable property on an empty object.
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
How It Works
- Define the field.
title is added with value "JS Tips". - Writable is true. Assignment is allowed, so the value can change.
- 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().
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)
How It Works
- Still readable. You can read
box.secret directly. - Not in keys.
enumerable: false keeps it out of Object.keys. - 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.
var box = {};
Object.defineProperty(box, "secret", {
value: 42,
writable: false,
enumerable: false,
configurable: false
});
box.secret = 100;
console.log(box.secret); // 42
How It Works
- Try to change it.
box.secret = 100 runs without throwing in non-strict mode. - Value unchanged. Because
writable is false, the value stays 42. - Strict mode note. In strict mode the same assignment would throw a
TypeError.
Compatibility
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 supportObject.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.
About the author
Developer, cloud engineer, and technical writer
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