JavaScript Proxy handler.ownKeys() Method

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

Definition

handler.ownKeys(target) is a trap that runs when JavaScript wants the list of keys of a Proxy. This happens with Object.keys(), Object.getOwnPropertyNames(), Reflect.ownKeys(), JSON.stringify(), the spread syntax and the for...in loop.

The trap returns an array of strings and symbols. Inside the trap you can remove keys to hide them, or add keys. To get the real list, use Reflect.ownKeys(target).

Remember
const handler = {
  ownKeys(target) { ... }
};

target  → the original object

return an array of strings and symbols
no duplicates, no numbers
must include the non-configurable keys of the target

Syntax

Add an ownKeys trap to the handler of a Proxy.

JavaScript
const user = new Proxy({ name: "Ann", _token: "abc" }, {
  ownKeys(target) {
    return Reflect.ownKeys(target).filter((key) => key !== "_token");
  }
});

console.log(Object.keys(user).join(", "));   // name
CallWhat it does
Object.keys(proxy), Object.values(proxy), Object.entries(proxy)List keys, values or pairs. They run the trap.
Reflect.ownKeys(proxy), Object.getOwnPropertyNames(proxy)List all own keys. They run the trap.
for (const key in proxy)The loop asks for the keys, so it runs the trap.
JSON.stringify(proxy), { ...proxy }Both read the keys first. They run the 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.preventExtensions(), handler.set(), handler.setPrototypeOf().

Library declarations
JavaScript
// ECMAScript 2015 (ES6)
handler.ownKeys(target)

// target  : the original object
// returns : an array of strings and symbols
// throws  : TypeError if the list breaks a rule

Parameters

ParameterDescription
targetThe original object that the Proxy wraps.

Return Value

Return ValueDescription
ArrayA list of strings and symbols with no duplicates. It becomes the list of own keys of the Proxy.
TypeError (thrown)The list has a number, a duplicate, or leaves out a non-configurable key of the target. If the target is not extensible, the list must hold exactly the keys of the target.

JavaScript Examples

Example 1 — Hide Private Keys from Every List

Remove the keys that start with an underscore from the list. Then Object.keys(), JSON.stringify() and for...in all agree.

JavaScript
const user = { name: "Ann", age: 30, _token: "abc" };

const view = new Proxy(user, {
  ownKeys(target) {
    return Reflect.ownKeys(target).filter((key) => {
      return typeof key !== "string" || !key.startsWith("_");
    });
  }
});

console.log("Object.keys: " + Object.keys(view).join(", "));
console.log("Reflect.ownKeys: " + Reflect.ownKeys(view).join(", "));
console.log("JSON: " + JSON.stringify(view));

const seen = [];
for (const key in view) {
  seen.push(key);
}
console.log("for...in: " + seen.join(", "));
console.log("Direct read: " + view._token);
Try It Yourself

How It Works

  1. Filter the real list. Reflect.ownKeys(target) gives all the keys. The trap keeps symbols and every string that does not start with "_".
  2. Every list agrees. Object.keys(), Reflect.ownKeys(), JSON.stringify() and for...in all start from the trap, so none of them show _token.
  3. Reading still works. The trap only changes the list. view._token still returns "abc", because reading uses the get trap.

Example 2 — Made-Up Keys Need a Descriptor Too

A trap can list keys that the target does not have. See why Object.keys() ignores them until a getOwnPropertyDescriptor trap helps.

JavaScript
const virtual = new Proxy({}, {
  ownKeys() {
    return ["a", "b"];
  }
});

console.log("Reflect.ownKeys: " + Reflect.ownKeys(virtual).join(", "));
console.log("Object.keys: " + Object.keys(virtual).length);

const complete = new Proxy({}, {
  ownKeys() {
    return ["a", "b"];
  },
  getOwnPropertyDescriptor() {
    return { value: 1, enumerable: true, configurable: true };
  }
});

console.log("Object.keys with both traps: " + Object.keys(complete).join(", "));
Try It Yourself

How It Works

  1. The list is made up. Reflect.ownKeys() returns exactly what the trap returns: a and b.
  2. Object.keys checks each key. It asks whether each key is enumerable. The empty target has no such properties, so both keys are skipped and the length is 0.
  3. Add the second trap. When getOwnPropertyDescriptor says the keys exist and are enumerable, Object.keys() includes them.

Example 3 — The Rules for the List

The list must hold only strings and symbols, with no duplicates, and it must keep the non-configurable keys of the target. The catch blocks print the error type only.

JavaScript
const dup = new Proxy({}, {
  ownKeys() {
    return ["a", "a"];
  }
});
try {
  Reflect.ownKeys(dup);
} catch (err) {
  console.log("Duplicate keys: " + err.name);
}

const num = new Proxy({}, {
  ownKeys() {
    return [1, 2];
  }
});
try {
  Reflect.ownKeys(num);
} catch (err) {
  console.log("Number keys: " + err.name);
}

const fixed = {};
Object.defineProperty(fixed, "id", { value: 1 });
const missing = new Proxy(fixed, {
  ownKeys() {
    return [];
  }
});
try {
  Reflect.ownKeys(missing);
} catch (err) {
  console.log("Missing a fixed key: " + err.name);
}
Try It Yourself

How It Works

  1. No duplicates. A key can only be listed once, so ["a", "a"] throws a TypeError.
  2. Strings and symbols only. Property keys are never numbers. Return ["1", "2"] with quotes, not [1, 2].
  3. Keep fixed keys. The property id is non-configurable, so it can never be hidden. A list without it throws a TypeError.

Browser & Runtime Support

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

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.ownKeys() 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 JavaScript needs the list of own keys of a Proxy, for example for Object.keys(), Reflect.ownKeys(), JSON.stringify() and for...in. It returns an array of strings and symbols.
Return the real keys without it, for example Reflect.ownKeys(target).filter(...). Object.keys() starts from your list, so the key disappears from it, and from JSON.stringify() and for...in too.
Object.keys() checks each key with getOwnPropertyDescriptor and skips the ones that do not exist on the target or are not enumerable. Add a getOwnPropertyDescriptor trap that returns an enumerable descriptor for them.
It must be an array-like list of strings and symbols, with no duplicates. It must include every non-configurable key of the target, and if the target is not extensible, it must contain exactly the keys of the target.
No. It only changes the list of keys. Reading a value uses the get trap, so hiding a key from the list does not stop someone who knows its name from reading it.

Next: handler.preventExtensions()

Learn how a Proxy sees the request to lock an object with the preventExtensions trap.

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

11 people found this page helpful