Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

in operator — checking property existence

The `in` operator checks for a property by key, not by value. For an ordinary object with an ordinary prototype chain, it checks for both own and inherited properties. A `Proxy`'s `has` trap can change the result. The right operand must be an object; a primitive causes `TypeError`.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

propertyKey in object
#privateName in object

If the object and every object in its prototype chain are ordinary objects, propertyKey in object returns true for an own or inherited property and false otherwise. The left operand is converted to a property key: a string or Symbol. The right operand must be an object; null, undefined, and primitive strings also cause TypeError.

A Proxy's has trap can intercept the property-key check, including during a prototype-chain lookup. For example, it can report a key as present even if neither the target nor its prototypes have it, or hide an existing configurable own property on an extensible target.

Existence does not mean a value other than undefined: a property with that value still exists. To check only own properties, use Object.hasOwn(object, propertyKey). The in operator also includes non-enumerable properties; it is not a for...in loop.

For arrays, in checks index keys, not whether a value occurs. A hole left by delete is not an own element, even when its index is below length. If no Proxy intercepts the check and the lookup does not find the key in the prototype chain either, the result is false.

The separate syntax #privateName in object checks for a particular private element, not a property with a string key. The private name must be declared in an enclosing class scope; the right operand must still be an object.

Examples

const parent = { inherited: 42 };
const item = Object.create(parent);
item.own = undefined;

console.log("own" in item);
console.log("inherited" in item);
console.log("missing" in item);
console.log(Object.hasOwn(item, "inherited"));

const values = [undefined];
console.log(0 in values);
delete values[0];
console.log(0 in values);

Result

true
true
false
false
true
false

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources