Reflect.defineProperty
Defines a new property directly on an object or modifies an existing property, returning a boolean indicating success
Syntax
Reflect.defineProperty(target, propertyKey, attributes)Parameters
| Parameter | Type | Description |
|---|---|---|
| target | object | The target object |
| propertyKey | string | symbol | The property name |
| attributes | PropertyDescriptor | The descriptor for the property |
Return Value
A boolean indicating whether the property was successfully defined
Examples
const obj: Record<string, unknown> = {}
Reflect.defineProperty(obj, 'name', {
value: 'Alice',
writable: false,
enumerable: true
})
console.log(obj.name) // 'Alice'const obj = {}
const success = Reflect.defineProperty(obj, 'x', {
get() { return 42 },
configurable: true
})
console.log(success) // trueconst frozen = Object.freeze({ a: 1 })
const result = Reflect.defineProperty(frozen, 'b', { value: 2 })
console.log(result) // false (object is frozen)Understanding Reflect.defineProperty
The Reflect.defineProperty method in JavaScript defines a new property directly on an object or modifies an existing property, returning a boolean indicating success. It belongs to the Reflect object and is one of the most widely used methods for working with reflect values in modern JavaScript and TypeScript applications.
The method signature is Reflect.defineProperty(target, propertyKey, attributes). It accepts 3 parameters: target, propertyKey, attributes. When called, it returns a boolean indicating whether the property was successfully defined. Understanding when and how to use defineProperty() helps you write more expressive, readable code.
Common use cases for Reflect.defineProperty include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like reflect-get, reflect-set, reflect-ownkeys, enabling you to chain operations together for complex data manipulation pipelines.
Browser support for Reflect.defineProperty is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.
Browser Compatibility
Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.
Related Methods
Reflect.getGets the value of a property on an object, similar to target[propertyKey] but as a function
Reflect.setSets the value of a property on an object, similar to target[propertyKey] = value but as a function
Reflect.ownKeysReturns an array of the target object's own property keys including non-enumerable and symbol properties
More Reflect Methods
Other methods in the Reflect object
Related Tools
More Reflect Methods
Explore JavaScript Methods
Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.