JavaScript Proxy.revocable() Method

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

Definition

Proxy.revocable(target, handler) creates a Proxy that can be switched off. It returns an object with two parts: proxy, which works like a normal Proxy, and revoke, a function that turns the Proxy off for good.

After you call revoke(), every use of the Proxy throws a TypeError. This is useful when you want to lend an object to some code for a short time and then take the access back. The original object is not changed or harmed.

Remember
const { proxy, revoke } = Proxy.revocable(target, handler);

proxy    → works like new Proxy(target, handler)
revoke() → switches the proxy off, for good

After revoke(): any use of the proxy throws TypeError
The target object itself is not affected
Calling revoke() again does nothing

Syntax

Use destructuring to take the two parts out of the result.

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

const { proxy, revoke } = Proxy.revocable(user, {});

console.log(proxy.name);   // Ann

revoke();
// proxy.name would now throw a TypeError
CallWhat it does
Proxy.revocable(target, handler)Create a Proxy and a revoke function. Returns { proxy, revoke }.
revoke()Switch the Proxy off. It takes no arguments and returns undefined.
new Proxy(target, handler)A normal Proxy. It can never be switched off.
Proxy.revocable(target, {})An empty handler: the Proxy just passes everything through to the target.

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

Library declarations
JavaScript
// ECMAScript 2015 (ES6)
Proxy.revocable(target, handler)

// target  : an object (or a function)
// handler : an object that holds the traps (it can be empty)
// returns : { proxy, revoke }
// throws  : TypeError if target or handler is not an object

Parameters

ParameterDescription
targetRequired. The object (or function) that the Proxy wraps. A number, a string or null throws a TypeError.
handlerRequired. An object that holds the traps, such as get or set. Use {} if you do not need any traps.

Return Value

Return ValueDescription
ObjectA plain object with two properties: proxy (the Proxy) and revoke (a function with no arguments).
TypeError (thrown)target or handler is not an object.

JavaScript Examples

Example 1 — Create a Proxy and Switch It Off

Use the proxy, call revoke(), and see that the proxy stops working while the original object is still fine. The catch block prints the error type only.

JavaScript
const target = { name: "Ann" };
const { proxy, revoke } = Proxy.revocable(target, {});

console.log("Before: " + proxy.name);
revoke();

try {
  console.log(proxy.name);
} catch (err) {
  console.log("After revoke: " + err.name);
}

console.log("Target still works: " + target.name);
Try It Yourself

How It Works

  1. Two parts. Proxy.revocable() returns { proxy, revoke }. The empty handler {} means the proxy simply passes reads to target, so proxy.name gives "Ann".
  2. Revoke. After revoke(), the proxy is dead. Reading proxy.name now throws a TypeError.
  3. Target is safe. Only the proxy was switched off. The original object target still returns its value.

Example 2 — Every Operation Fails After revoke()

Try different actions on a revoked proxy. Each one throws a TypeError. Calling revoke() again is harmless, and a number is not a valid target.

JavaScript
const { proxy, revoke } = Proxy.revocable({ id: 1 }, {});

console.log("typeof before: " + typeof proxy);
console.log("First revoke: " + revoke());
console.log("Second revoke: " + revoke());
console.log("typeof after: " + typeof proxy);

const tests = {
  read: () => proxy.id,
  write: () => { proxy.id = 2; },
  check: () => "id" in proxy,
  remove: () => delete proxy.id
};

const results = [];
Object.keys(tests).forEach((name) => {
  try {
    tests[name]();
    results.push(name + ": ok");
  } catch (err) {
    results.push(name + ": " + err.name);
  }
});
console.log(results.join(", "));

try {
  Proxy.revocable(5, {});
} catch (err) {
  console.log("Bad target: " + err.name);
}
Try It Yourself

How It Works

  1. The type stays. revoke() returns undefined, and typeof proxy says "object" before and after. Calling revoke() a second time does nothing.
  2. Nothing works. Reading, writing, the in check and delete all throw a TypeError on a revoked proxy. The loop collects one result per action.
  3. Valid target needed. A number is not an object, so Proxy.revocable(5, {}) throws a TypeError straight away.

Example 3 — Give Temporary Access to an Object

A handler can log every read. Once the proxy is revoked, the trap is not called any more.

JavaScript
const data = { token: "abc123" };
const logs = [];

const handler = {
  get(target, key, receiver) {
    logs.push("read " + String(key));
    return Reflect.get(target, key, receiver);
  }
};

const { proxy: view, revoke } = Proxy.revocable(data, handler);

console.log("Value: " + view.token);
revoke();

let after;
try {
  after = view.token;
} catch (err) {
  after = err.name;
}

console.log("After revoke: " + after);
console.log("Trap calls: " + logs.length);
console.log("Log: " + logs.join(", "));
Try It Yourself

How It Works

  1. A watching handler. The get trap saves a note for each read and then forwards the read with Reflect.get(). The destructuring { proxy: view, revoke } renames the proxy to view.
  2. One read, one note. While the proxy is alive, view.token returns the value and the log gets one entry.
  3. No more traps. After revoke(), the read throws a TypeError before your trap can run, so the log still has only one entry. This is how you can lend an object for a short time.

Browser & Runtime Support

Proxy.revocable() 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

Proxy.revocable()

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
Proxy.revocable() 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 creates a Proxy together with a revoke function. You use the proxy like any other Proxy, and when you call revoke(), the proxy is switched off and every use of it throws a TypeError.
Both make a Proxy that wraps a target with a handler. new Proxy() returns only the proxy, which stays alive. Proxy.revocable() returns an object with the proxy and a revoke function, so you can end its life.
No. A revoked proxy stays dead for good. If you need access again, call Proxy.revocable() to create a new proxy for the same target.
When you give an object to code that you do not fully control, for a limited time, for example a plugin or a temporary session. After you revoke it, the code can no longer reach your object through the proxy.
No. It only switches off the proxy. The target keeps all its properties and values, and any code that holds the target directly can still use it.

Next: handler.apply()

Learn how a Proxy can watch and change function calls with the apply trap.

handler.apply() 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.

10 people found this page helpful