Reflect

Reflect.defineProperty

Defines a new property directly on an object or modifies an existing property, returning a boolean indicating success

Syntax

JavaScript
Reflect.defineProperty(target, propertyKey, attributes)

Parameters

ParameterTypeDescription
targetobjectThe target object
propertyKeystring | symbolThe property name
attributesPropertyDescriptorThe descriptor for the property

Return Value

A boolean indicating whether the property was successfully defined

Examples

Basic Usage
const obj: Record<string, unknown> = {}
Reflect.defineProperty(obj, 'name', {
  value: 'Alice',
  writable: false,
  enumerable: true
})
console.log(obj.name) // 'Alice'
Practical Example
const obj = {}
const success = Reflect.defineProperty(obj, 'x', {
  get() { return 42 },
  configurable: true
})
console.log(success) // true
Advanced Usage
const 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

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.