Reflect.defineProperty(target, propertyKey, attributes)adds a new property to an object, or changes an existing one, using a descriptor that says how the property should behave: its value, and whether it is writable, enumerable and configurable.
It works like Object.defineProperty() with one big difference. When the change is not allowed, Object.defineProperty() throws an error, but Reflect.defineProperty() simply returns false. When it works, it returns true.
Remember
Reflect.defineProperty(target, key, descriptor)
changed or created → true
not allowed → false (no error)
Missing flags default to false:
writable, enumerable, configurable
target must be an object, or TypeError
Syntax
Pass an object, the property name, and a descriptor object. Check the returned boolean.
// ECMAScript 2015 (ES6)
Reflect.defineProperty(target, propertyKey, attributes)
// target : an object
// propertyKey : a string or a symbol
// attributes : a descriptor object
// returns : true or false
// throws : TypeError if target is not an object
Parameters
Parameter
Description
target
Required. The object to change. If it is not an object, a TypeError is thrown.
propertyKey
Required. The name of the property: a string or a symbol.
attributes
Required. A descriptor object with some of value, writable, get, set, enumerable and configurable. You cannot mix value/writable with get/set.
Return Value
Return Value
Description
true
The property was created or changed.
false
The change is not allowed, for example because the object is frozen or the property is not configurable.
TypeError (thrown)
target is not an object, or attributes is not a valid descriptor.
JavaScript Examples
Example 1 — Define a Read-Only Property
Create a property with only a value, and see which flags JavaScript sets to false for you.
Create: true
Change value: false
Same value again: true
Make writable: false
Final value: 10
Change a normal property: true
count is now: 5
How It Works
Locked after creation. The first call creates limit with writable and configurable set to false, so later changes are checked strictly.
Only harmless changes pass. A new value, or turning writable back on, is refused with false. Repeating the same value is allowed and returns true.
A normal property is flexible.count was created by assignment, so all its flags are true and the change works.
Compatibility
Browser & Runtime Support
Reflect.defineProperty() is part of ES2015 (ES6), together with the Reflect object. Every current browser and Node.js has it.
✓ ES2015 · Widely supported
Reflect.defineProperty()
Supported in Chrome 49+, Edge 12+, Firefox 42+, Safari 10+, Opera 36+ and Node.js 6+. Internet Explorer never got it.
ES2015Standard since
Google Chrome49+ · Desktop & Mobile
Full support
Microsoft Edge12+ · Chromium and legacy
Full support
Mozilla Firefox42+ · Desktop & Mobile
Full support
Apple Safari10+ · macOS & iOS
Full support
Opera36+ · Chromium based
Full support
Samsung InternetRecent versions · Android
Supported
Android WebView49+ · Chromium based
Supported
Node.js6+ · Server runtime
Supported
DenoSupported · JavaScript runtime
Supported
BunSupported · JavaScript runtime
Supported
Internet ExplorerNo native support
Not supported
Reflect.defineProperty()Excellent
Bottom line: Safe to use in every current browser and in Node.js. The only thing to plan for is Internet Explorer, which has no Reflect at all.
Frequently Asked Questions
It adds a property to an object or changes an existing one, using a descriptor. It returns true if that worked and false if it was not allowed.
Object.defineProperty() returns the object and throws a TypeError when the change is not allowed. Reflect.defineProperty() returns true or false instead, so you can check the result with a simple if statement.
Flags that you leave out of the descriptor are false for a new property. enumerable is false by default, so the property is hidden from those loops. Add enumerable: true to show it.
Yes. Use get and set functions in the descriptor instead of value and writable, for example { get() { return 5; } }. You cannot use value and get in the same descriptor.
It returns false when the object is not extensible and the property is new, or when the existing property is not configurable and you try to change it in a way that is not allowed.