JavaScript Proxy handler.has() Method

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

Definition

handler.has(target, property) is a trap that runs when you use the in operator on a Proxy, for example "name" in proxy. It is also used by Reflect.has(). The trap returns true or false.

Inside the trap you can hide properties, pretend that other properties exist, or build things like a number range. To answer in the normal way, use Reflect.has(target, property).

Remember
const handler = {
  has(target, property) { ... }
};

target    → the original object
property  → the name (a string or a symbol)

return true  → "property in proxy" is true
return false → "property in proxy" is false

Syntax

Add a has trap to the handler of a Proxy.

JavaScript
const user = new Proxy({ name: "Ann", _token: "abc" }, {
  has(target, key) {
    if (typeof key === "string" && key.startsWith("_")) {
      return false;   // hide private names
    }
    return Reflect.has(target, key);
  }
});

console.log("name" in user);     // true
console.log("_token" in user);   // false
CallWhat it does
"a" in proxyThe in operator runs the trap.
Reflect.has(proxy, "a")The function-style version.
"a" in Object.create(proxy)An object that inherits from the Proxy also runs the trap when the property is not found on it.
proxy.hasOwnProperty("a")Does not run this trap. It uses getOwnPropertyDescriptor.

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.isExtensible(), handler.ownKeys(), handler.preventExtensions(), handler.set(), handler.setPrototypeOf().

Library declarations
JavaScript
// ECMAScript 2015 (ES6)
handler.has(target, property)

// target   : the original object
// property : a string or a symbol
// returns  : true or false

Parameters

ParameterDescription
targetThe original object that the Proxy wraps.
propertyThe name that is being checked: a string or a symbol. A number such as 5 in proxy arrives as the string "5".

Return Value

Return ValueDescription
trueThe in operator reports that the property exists.
falseThe in operator reports that it does not exist.
TypeError (thrown)The trap hid a property that the target cannot lose, such as a non-configurable property.

JavaScript Examples

Example 1 — Hide Private Names from the in Operator

Return false for names that start with an underscore. The value can still be read, because reading uses a different trap.

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

const view = new Proxy(user, {
  has(target, key) {
    if (typeof key === "string" && key.startsWith("_")) {
      return false;
    }
    return Reflect.has(target, key);
  }
});

console.log("name in view: " + ("name" in view));
console.log("_token in view: " + ("_token" in view));
console.log("Reflect.has: " + Reflect.has(view, "_token"));
console.log("Read _token: " + view._token);
console.log("Own check: " + view.hasOwnProperty("_token"));
Try It Yourself

How It Works

  1. Hide by name. For names that start with "_", the trap returns false. Reflect.has() runs the same trap, so it also says false.
  2. Others are normal. name goes to Reflect.has(target, key) and is found.
  3. Only in is affected. Reading view._token still works, and hasOwnProperty() still says true, because they use other traps.

Example 2 — Make a Number Range with in

A Proxy of an empty object can still answer the in operator. This one says that every whole number from 1 to 10 is "in" it.

JavaScript
const range = new Proxy({}, {
  has(target, key) {
    if (typeof key !== "string") {
      return false;
    }
    const n = Number(key);
    return Number.isInteger(n) && n >= 1 && n <= 10;
  }
});

console.log("5 in range: " + (5 in range));
console.log("10 in range: " + (10 in range));
console.log("11 in range: " + (11 in range));
console.log("0 in range: " + (0 in range));
console.log("2.5 in range: " + (2.5 in range));
console.log("abc in range: " + ("abc" in range));
Try It Yourself

How It Works

  1. Keys are strings. The number 5 arrives as "5". The trap turns it back with Number(key).
  2. No real data. The target is an empty object. The trap makes up all the answers, which is allowed because the target has no locked properties.
  3. Rules for the answer. Only whole numbers from 1 to 10 give true. 2.5 and "abc" are not whole numbers, so they give false.

Example 3 — Count the Checks and Follow the Rules

Count how many times the trap runs, and see which other operations do not run it. Then see that a trap cannot hide a frozen property. The catch block prints the error type only.

JavaScript
let checks = 0;
const counted = new Proxy({ a: 1 }, {
  has(target, key) {
    checks++;
    return Reflect.has(target, key);
  }
});

"a" in counted;
"b" in counted;
Reflect.has(counted, "a");
console.log("Checks: " + checks);

counted.hasOwnProperty("a");
Object.keys(counted);
console.log("After hasOwnProperty and Object.keys: " + checks);

const frozen = Object.freeze({ x: 1 });
const liar = new Proxy(frozen, {
  has() {
    return false;
  }
});

try {
  "x" in liar;
} catch (err) {
  console.log("Hiding a frozen property: " + err.name);
}
Try It Yourself

How It Works

  1. Three checks. Two uses of the in operator and one Reflect.has() call run the trap, so the counter is 3.
  2. Other operations skip it. hasOwnProperty() and Object.keys() use different traps, so the counter does not move.
  3. No lying about frozen. x is non-configurable and cannot disappear. Answering false makes JavaScript throw a TypeError.

Browser & Runtime Support

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

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.has() 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 the in operator or Reflect.has() is used on a Proxy. It receives the target and the property name and returns true or false.
No. Those use the getOwnPropertyDescriptor and ownKeys traps. The has trap is only for the in operator, Reflect.has() and the with statement.
No. proxy.name still returns the value, because reading uses the get trap. To hide a property fully, add traps for get, ownKeys and getOwnPropertyDescriptor too.
Property names in JavaScript are always strings or symbols. 5 in proxy becomes the string "5", so convert it with Number(key) if you need a number.
Yes. Because the answer is made by your code, the target can be empty, as in the number range example. This is a fun way to give the in operator a custom meaning.

Next: handler.isExtensible()

Learn how a Proxy sees the question "can new properties be added?" with the isExtensible trap.

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

9 people found this page helpful