Reflect.set(target, propertyKey, value)assigns a value to a property of an object. It does the same as target[propertyKey] = value, but as a function, and it returns true or false to tell you whether it worked.
When the assignment is not allowed (for example, the property is read-only), the normal = is silently ignored in sloppy mode or throws in strict mode. Reflect.set() never throws for this. It just returns false.
Remember
Reflect.set(target, key, value)
assigned → true
not allowed → false (no error)
Same as: target[key] = value
Optional 4th argument: receiver (the this for setters)
target must be an object, or TypeError
Syntax
Pass the object, the property name and the new value. Use the returned boolean to see if it worked.
// ECMAScript 2015 (ES6)
Reflect.set(target, propertyKey, value)
Reflect.set(target, propertyKey, value, receiver)
// target : an object
// propertyKey : a string or a symbol
// value : any value
// receiver : optional, defaults to target
// 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.
value
Required. The new value to assign.
receiver
Optional. The object that is used as this when the property is a setter. By default it is target.
Return Value
Return Value
Description
true
The assignment worked.
false
The assignment was not allowed, for example for a read-only property, a frozen object or a getter without a setter.
TypeError (thrown)
target is not an object.
JavaScript Examples
Example 1 — Set Properties of Objects and Arrays
Change a property, add a new one and set an array position. Each call returns true.
Change name: true
Add age: true
User: {"name":"Bob","age":30}
Set position 2: true
List: a, b, c
Length: 3
How It Works
Change and add.Reflect.set() replaces name and creates age. Both return true.
Arrays work too. Setting position 2 adds a third item, just like list[2] = "c".
The length updates. The array's length grew to 3 automatically.
Example 2 — When Reflect.set() Returns false
See the cases that do not work: a read-only property, a frozen object and a property with a getter only. In strict mode, the plain assignment throws, but Reflect.set() does not.
Result: true
Stored on target: 10
Stored on owner: undefined
Default receiver: true
Stored on owner now: 5
Number target: TypeError
How It Works
A setter with this. The setter total saves the value in this._total. Which object that is depends on the receiver.
Custom receiver. With target as the 4th argument, the value lands on target, and owner stays empty.
Default receiver. Without the 4th argument, this is the target itself, so owner._total becomes 5. A number as target throws a TypeError.
Compatibility
Browser & Runtime Support
Reflect.set() is part of ES2015 (ES6), together with the Reflect object. Every current browser and Node.js has it.
✓ ES2015 · Widely supported
Reflect.set()
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.set()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 assigns a value to a property of an object, like obj[key] = value, but as a function that returns true if it worked and false if it did not.
It tells you whether the assignment worked, it never throws for a read-only property in strict mode, and you can pass a receiver. It is also the right way to forward the assignment inside a Proxy set trap.
It is the object that is used as this when the property has a setter. By default it is the target. It matters when the target is in the prototype chain of another object, as it is inside a Proxy.
When the assignment is not allowed: the property is read-only, the object is frozen or not extensible, or the property is a getter without a setter.
In a set trap, call return Reflect.set(target, key, value, receiver) after your own checks. This does the normal assignment and gives the correct true or false result back to the Proxy.