Path: blob/main/files/en-us/web/javascript/reference/operators/in/index.md
6552 views
------{{jsSidebar("Operators")}}
The in operator returns true if the specified property is in the specified object or its prototype chain.
{{EmbedInteractiveExample("pages/js/expressions-inoperator.html")}}
Syntax
Parameters
prop: A string or symbol representing a property name (non-symbols will be coerced to strings). Can also be a private property identifier.
object: Object to check if it (or its prototype chain) contains the property with specified name (
prop).
Exceptions
{{jsxref("TypeError")}}
: Thrown if
objectis not an object (i.e. a primitive).
Description
The in operator tests if a string or symbol property is present in an object or its prototype chain. If you want to check for only non-inherited properties, use {{jsxref("Object.hasOwn()")}} instead.
A property may be present in an object but have value undefined. Therefore, x in obj is not the same as obj.x === undefined. To make in return false after a property is added, use the delete operator instead of setting that property's value to undefined.
You can also use the in operator to check whether a particular private class field or method has been defined in an object. The operator returns true if the property is defined, and false otherwise. This is known as a branded check, because it returns true if and only if the object was created with that class constructor, after which you can safely access other private properties as well.
This is a special syntax — the left-hand side of the in operator is a property identifier instead of an expression, but unquoted (because otherwise it's a string property, not a private property).
Because accessing private properties on objects unrelated to the current class throws a {{jsxref("TypeError")}} instead of returning undefined, this syntax allows you to shorten:
To:
It also generally avoids the need for dealing with error handling just to access a private property that may be nonexistent.
However, the in operator still requires the private property to be declared beforehand in the enclosing class — otherwise, it would throw a {{jsxref("SyntaxError")}} ("Private field '#x' must be declared in an enclosing class"), the same one as when you try to access an undeclared private property.
Examples
Basic usage
The following examples show some uses of the in operator.
You must specify an object on the right side of the in operator. For example, you can specify a string created with the String constructor, but you cannot specify a string literal.
Using the in operator with deleted or undefined properties
If you delete a property with the delete operator, the in operator returns false for that property.
If you set a property to {{jsxref("Global_Objects/undefined", "undefined")}} but do not delete it, the in operator returns true for that property.
The in operator will return false for empty array slots, even if accessing it directly returns undefined.
To avoid this, make sure a new array is always filled with non-empty values or not write to indexes past the end of array.
Inherited properties
The in operator returns true for properties in the prototype chain. This may be undesirable if you are using objects to store arbitrary key-value pairs.
You can use {{jsxref("Object.hasOwn()")}} to check if the object has the key.
Alternatively, you should consider using a null prototype object or a {{jsxref("Map")}} for storing ages, to avoid other bugs.
Using the in operator to implement branded checks
The code fragment below demonstrates a static function that tells if an object was created with the Person constructor and therefore can perform other methods safely.
It helps to prevent the following case:
Without the in operator, you would have to use a try...catch block to check if the object has the private property.
You can also implement this as a @@hasInstance method of the class, so that you can use the instanceof operator to perform the same check (which, by default, only checks for the existence of Person.prototype in the object's prototype chain).
For more examples, see Private class features and the class guide.
Specifications
{{Specifications}}
Browser compatibility
{{Compat}}
See also
{{jsxref("Object.hasOwn()")}}
{{jsxref("Reflect.has()")}}