JavaScript (ES2026) reference · Object Methods

JavaScript Object.defineProperty() Method

Add or change one property with full control over whether it is writable, enumerable and configurable, or define a getter.

JavaScript (ES2026) Since ES5

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

Object.defineProperty(obj, prop, descriptor)

Parameters

ParameterDescription
obj The object to inspect or change.
prop The property name.
descriptor { value, writable, enumerable, configurable } or { get, set }.

Return value

The object.

Examples

A read-only propertyTry it yourself
"use strict";
const config = {};
Object.defineProperty(config, "version", { value: "1.0", writable: false, enumerable: true });
try { config.version = "2.0"; } catch (e) { console.log(e.constructor.name); }
console.log(config.version);

Output

TypeError
1.0
A computed, non-enumerable getterTry it yourself
const temp = { celsius: 20 };
Object.defineProperty(temp, "fahrenheit", { get() { return this.celsius * 9 / 5 + 32; } });
console.log(temp.fahrenheit, Object.keys(temp));

Output

68 [ 'celsius' ]

Version history

VersionChange
JavaScript ES5Added

Was this page helpful?