Skip to main content
The in operator is a relational operator that evaluates to true if a specified property key or private identifier exists within a given object or anywhere along its [[Prototype]] chain.

Syntax

  • property / #privateIdentifier: Either an expression that evaluates to a String or a Symbol representing the property name, or a literal Private Identifier (e.g., #prop). If an expression evaluates to a non-string/non-symbol value, it is implicitly coerced to a string.
  • object: The target object to inspect.

Technical Mechanics

1. Prototype Chain Traversal Unlike Object.hasOwn() or Object.prototype.hasOwnProperty(), the in operator does not restrict its search to the object’s own properties. If the property is not found directly on the object, the JavaScript engine traverses the object’s prototype chain until it either finds the property or reaches the end of the chain (null).
2. Right Operand Constraints The right operand must be an object type (e.g., Object, Array, Function). Passing a primitive value (string, number, boolean, null, or undefined) as the right operand throws a TypeError.
3. Array Index Evaluation and Sparse Arrays When applied to arrays, the in operator evaluates the presence of the index (the property key), not the value stored at that index. Crucially, the in operator returns false for empty slots in sparse arrays, distinguishing them from slots explicitly assigned undefined.
4. Handling of undefined vs. Deleted Properties The in operator checks for the presence of the key, regardless of the value associated with it. If a property is explicitly set to undefined, the operator returns true. It only returns false if the property was never defined or was explicitly removed using the delete operator.
5. Private Field Checking (Ergonomic Brand Checking) Introduced in ES2022, the in operator natively supports checking for the presence of private class fields, methods, or accessors. When used this way, the left operand must be a literal Private Identifier syntax, not a string.
6. Symbol Resolution The in operator natively supports Symbol primitives as the left operand without attempting string coercion.
Tired of Poor JavaScript Skills? Fix That With Deep Grasping!Learn More