handler.set(target, property, value, receiver) is a trap that runs when you assign a value to a property of a Proxy, for example proxy.age = 30. It is the best place to validate data, because every change goes through it.
The trap returns true when the assignment worked and false when it did not. Inside the trap you can check the value, change it, block the change, or log it. To store the value in the normal way, use Reflect.set(target, property, value, receiver).
Remember
const handler = {
set(target, property, value, receiver) { ... }
};
target → the original object
property → the name (a string or a symbol)
value → the new value
receiver → the proxy (or an object that inherits from it)
return true → the assignment worked
return false → refused (strict mode: TypeError)
Syntax
Add a set trap to the handler of a Proxy. Return a boolean.
JavaScript
const person = new Proxy({}, {
set(target, key, value, receiver) {
if (key === "age" && typeof value !== "number") {
throw new TypeError("age must be a number");
}
return Reflect.set(target, key, value, receiver);
}
});
person.age = 30; // works
// person.age = "x"; // would throw a TypeError
Call
What it does
proxy.name = "Ann"
Assigning with a dot runs the trap.
proxy["name"] = "Ann", proxy[0] = "a"
Assigning with square brackets runs it too. Numbers arrive as strings.
Reflect.set(proxy, "name", "Ann")
The function-style version. It returns true or false.
proxy.count++
A change like ++ first reads (get trap) and then writes (set trap).
// ECMAScript 2015 (ES6)
handler.set(target, property, value, receiver)
// target : the original object
// property : a string or a symbol
// value : the new value
// receiver : the proxy, or an object that inherits from it
// returns : true or false
Parameters
Parameter
Description
target
The original object that the Proxy wraps.
property
The name of the property that is being assigned: a string or a symbol. A number such as list[0] arrives as the string "0".
value
The new value that is being assigned.
receiver
The object that the assignment was made on. It is normally the Proxy itself. Pass it on to Reflect.set().
Return Value
Return Value
Description
true
The assignment worked.
false
The assignment is refused. In normal code it is ignored. In strict mode (and in classes and modules) it throws a TypeError.
TypeError (thrown)
The trap broke a rule, for example by returning true for a read-only, non-configurable property of the target with a different value.
JavaScript Examples
Example 1 — Validate a Value Before It Is Stored
Reject a value that is not a whole number. The error message tells the user what went wrong. The catch block prints the error type and message.
JavaScript
const person = new Proxy({}, {
set(target, key, value, receiver) {
if (key === "age" && !Number.isInteger(value)) {
throw new TypeError("age must be a whole number");
}
return Reflect.set(target, key, value, receiver);
}
});
person.age = 30;
console.log("age: " + person.age);
try {
person.age = "old";
} catch (err) {
console.log(err.name + ": " + err.message);
}
console.log("age still: " + person.age);
person.name = "Ann";
console.log("name: " + person.name);
Old and new. The trap reads target[key] before the change, so each note shows the old value and the new one. A new name such as label has no old value, so it shows undefined.
Forgetting return. The second Proxy stores the value but returns nothing. In normal code that is ignored, so the value is stored.
Strict mode notices. No return means undefined, which counts as false. Strict code then throws a TypeError. Always return the result of Reflect.set().
Compatibility
Browser & Runtime Support
handler.set() is part of ES2015 (ES6), together with the Proxy object and the Reflect object. Every current browser and Node.js has it.
✓ ES2015 · Widely supported
handler.set()
Supported in Chrome 49+, Edge 12+, Firefox 49+, 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 Firefox49+ · 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
handler.set()Excellent
Bottom line: Safe to use in every current browser and in Node.js. A Proxy cannot be faked in old browsers, so do not use it where Internet Explorer must work.
Frequently Asked Questions
It is a method of the handler object that runs when a property of a Proxy is assigned, for example proxy.age = 30. It receives the target, the property name, the new value and the receiver, and it returns true or false.
Add a set trap that checks the value. If it is not valid, throw an error with a clear message. If it is valid, store it with Reflect.set(target, property, value, receiver) and return the result.
The assignment is refused. In normal code it is silently ignored. In strict mode, in classes and in modules, it throws a TypeError. Reflect.set() simply returns false.
Because the return value tells JavaScript whether the assignment worked. If you return nothing, undefined counts as false, and strict code will throw a TypeError even though the value may have been stored.
It is the object that the assignment was made on, normally the Proxy. Pass it to Reflect.set() so that setters and inherited properties behave correctly.