JavaScript Proxy handler.set() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
Proxy trap

Definition

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
CallWhat 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).

A standard JavaScript Proxy trap (ES2015). You write it inside the handler object that you pass to new Proxy(). JavaScript calls it for you. No import required. Siblings: Proxy.revocable(), handler.apply(), handler.construct(), handler.defineProperty(), handler.deleteProperty(), handler.get(), handler.getOwnPropertyDescriptor(), handler.getPrototypeOf(), handler.has(), handler.isExtensible(), handler.ownKeys(), handler.preventExtensions(), handler.setPrototypeOf().

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

ParameterDescription
targetThe original object that the Proxy wraps.
propertyThe name of the property that is being assigned: a string or a symbol. A number such as list[0] arrives as the string "0".
valueThe new value that is being assigned.
receiverThe object that the assignment was made on. It is normally the Proxy itself. Pass it on to Reflect.set().

Return Value

Return ValueDescription
trueThe assignment worked.
falseThe 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);
Try It Yourself

How It Works

  1. Check only what matters. The trap tests the age property. All other names skip the check.
  2. Throw to explain. For "old", the trap throws a TypeError with a clear message, and the old value stays, so age is still 30.
  3. Store the rest. Reflect.set() stores good values, and returns true, which is the answer of the trap.

Example 2 — Make a Property Read-Only

Return false for id. See how a plain assignment, Reflect.set() and strict mode react. The catch block prints the error type only.

JavaScript
const locked = new Proxy({ id: 1, name: "Ann" }, {
  set(target, key, value, receiver) {
    if (key === "id") {
      return false;
    }
    return Reflect.set(target, key, value, receiver);
  }
});

locked.name = "Bob";
locked.id = 2;
console.log("name: " + locked.name + ", id: " + locked.id);

console.log("Reflect.set id: " + Reflect.set(locked, "id", 3));

function strictSet(obj) {
  "use strict";
  obj.id = 4;
}
try {
  strictSet(locked);
} catch (err) {
  console.log("Strict assignment: " + err.name);
}
Try It Yourself

How It Works

  1. One protected name. The trap returns false for id, so the change is refused. name changes as normal.
  2. Quiet in normal code. locked.id = 2 is ignored, so id is still 1. Reflect.set() shows the refusal as false.
  3. Loud in strict mode. The same refused assignment inside a strict function throws a TypeError.

Example 3 — Log Changes and Do Not Forget return

Record each change as "old -> new". Then see what happens when a trap forgets to return true. The catch block prints the error type only.

JavaScript
const changes = [];
const watched = new Proxy({ count: 0 }, {
  set(target, key, value, receiver) {
    changes.push(key + ": " + target[key] + " -> " + value);
    return Reflect.set(target, key, value, receiver);
  }
});

watched.count = 1;
watched.count = 2;
watched.label = "x";
console.log(changes.join(" | "));

const forgetful = new Proxy({}, {
  set(target, key, value) {
    target[key] = value;
  }
});

forgetful.a = 1;
console.log("Stored anyway: " + forgetful.a);

function strictSet(obj) {
  "use strict";
  obj.b = 2;
}
try {
  strictSet(forgetful);
} catch (err) {
  console.log("Forgot to return true: " + err.name);
}
Try It Yourself

How It Works

  1. 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.
  2. Forgetting return. The second Proxy stores the value but returns nothing. In normal code that is ignored, so the value is stored.
  3. Strict mode notices. No return means undefined, which counts as false. Strict code then throws a TypeError. Always return the result of Reflect.set().

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.

ES2015 Standard since
Google Chrome 49+ · Desktop & Mobile
Full support
Microsoft Edge 12+ · Chromium and legacy
Full support
Mozilla Firefox 49+ · 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
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.

Next: handler.setPrototypeOf()

Learn how a Proxy can watch or block changes of the prototype with the setPrototypeOf trap.

handler.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.

17 people found this page helpful