JavaScript Reflect.set() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
Static method

Definition

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.

JavaScript
const user = { name: "Ann" };

console.log(Reflect.set(user, "name", "Bob"));   // true
console.log(Reflect.set(user, "age", 30));       // true

console.log(user.name + ", " + user.age);        // Bob, 30
CallWhat it does
Reflect.set(obj, "a", 1)Assign 1 to the property a. Returns a boolean.
Reflect.set(arr, 0, "x")Assign to an array position.
Reflect.set(obj, "a", 1, other)The 4th argument is the receiver: the object that setters use as this, and that receives the value.
obj.a = 1The operator. It never tells you if it failed.

A standard JavaScript static method (ES2015) of the Reflect object: call it as Reflect.set(). No import required. Siblings: Reflect.apply(), Reflect.construct(), Reflect.defineProperty(), Reflect.deleteProperty(), Reflect.get(), Reflect.getOwnPropertyDescriptor(), Reflect.getPrototypeOf(), Reflect.has(), Reflect.isExtensible(), Reflect.ownKeys(), Reflect.preventExtensions(), Reflect.setPrototypeOf().

Library declarations
JavaScript
// 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

ParameterDescription
targetRequired. The object to change. If it is not an object, a TypeError is thrown.
propertyKeyRequired. The name of the property: a string or a symbol.
valueRequired. The new value to assign.
receiverOptional. The object that is used as this when the property is a setter. By default it is target.

Return Value

Return ValueDescription
trueThe assignment worked.
falseThe 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.

JavaScript
const user = { name: "Ann" };

console.log("Change name: " + Reflect.set(user, "name", "Bob"));
console.log("Add age: " + Reflect.set(user, "age", 30));
console.log("User: " + JSON.stringify(user));

const list = ["a", "b"];
console.log("Set position 2: " + Reflect.set(list, 2, "c"));
console.log("List: " + list.join(", "));
console.log("Length: " + list.length);
Try It Yourself

How It Works

  1. Change and add. Reflect.set() replaces name and creates age. Both return true.
  2. Arrays work too. Setting position 2 adds a third item, just like list[2] = "c".
  3. 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.

JavaScript
const settings = { theme: "dark" };
Object.defineProperty(settings, "version", { value: 1, writable: false });

console.log("Read-only: " + Reflect.set(settings, "version", 2));
console.log("Still: " + settings.version);

const frozen = Object.freeze({ a: 1 });
console.log("Frozen: " + Reflect.set(frozen, "a", 2));

const view = { get label() { return "fixed"; } };
console.log("Getter only: " + Reflect.set(view, "label", "new"));

function strictAssign(obj) {
  "use strict";
  obj.version = 3;
}
try {
  strictAssign(settings);
} catch (err) {
  console.log("Strict assignment: " + err.name);
}
Try It Yourself

How It Works

  1. Read-only. The property version is not writable, so Reflect.set() returns false and the value stays 1.
  2. Frozen and getter-only. A frozen object accepts no changes, and a property that only has a getter cannot be assigned. Both give false.
  3. Compare strict mode. The same failed assignment with "use strict" throws a TypeError, while Reflect.set() stays quiet.

Example 3 — Setters and the Receiver

A setter runs with this set to the receiver. Use the 4th argument to store the value on a different object.

JavaScript
const owner = {
  set total(value) {
    this._total = value;
  }
};

const target = {};
console.log("Result: " + Reflect.set(owner, "total", 10, target));
console.log("Stored on target: " + target._total);
console.log("Stored on owner: " + owner._total);

console.log("Default receiver: " + Reflect.set(owner, "total", 5));
console.log("Stored on owner now: " + owner._total);

try {
  Reflect.set(5, "a", 1);
} catch (err) {
  console.log("Number target: " + err.name);
}
Try It Yourself

How It Works

  1. A setter with this. The setter total saves the value in this._total. Which object that is depends on the receiver.
  2. Custom receiver. With target as the 4th argument, the value lands on target, and owner stays empty.
  3. Default receiver. Without the 4th argument, this is the target itself, so owner._total becomes 5. A number as target throws a TypeError.

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.

ES2015 Standard since
Google Chrome 49+ · Desktop & Mobile
Full support
Microsoft Edge 12+ · Chromium and legacy
Full support
Mozilla Firefox 42+ · Desktop & Mobile
Full support
Apple Safari 10+ · macOS & iOS
Full support
Opera 36+ · Chromium based
Full support
Samsung Internet Recent versions · Android
Supported
Android WebView 49+ · Chromium based
Supported
Node.js 6+ · Server runtime
Supported
Deno Supported · JavaScript runtime
Supported
Bun Supported · JavaScript runtime
Supported
Internet Explorer No 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.

Next: Reflect.setPrototypeOf()

Learn how to change the prototype of an object and get true or false back.

Reflect.setPrototypeOf() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful