in operator
Returns `true` if the specified property is in the specified object or its prototype chain, otherwise returns `false`.
"propertyName" in objectNameThis static page keeps the syntax and examples indexed for search, while the coding app handles interactive exploration and saved references.
What it does
Overview
Returns `true` if the specified property is in the specified object or its prototype chain, otherwise returns `false`.
The `in` operator checks for the existence of a property in an object. It returns `true` if the property exists directly on the object or anywhere in its prototype chain. This is a key distinction from checking if a property's value is `undefined`, as a property can exist but have an `undefined` value. The `in` operator is useful for determining if an object "has" a certain property, regardless of its value. It's often used when iterating over object properties or when ensuring a specific configuration key is present. It can also be used with arrays to check if an index exists. For checking only own properties (not inherited ones), `Object.prototype.hasOwnProperty()` is preferred. The time complexity is generally O(1) for direct properties and O(k) for inherited properties, where k is the depth of the prototype chain.
Quick reference
Syntax
"propertyName" in objectName
See it in practice
Examples
Checking for own properties
const car = { make: "Honda", model: "Civic" };
console.log("make" in car);
console.log("year" in car);
console.log("toString" in car); // Inherited from Object.prototypetrue false true
"make" is an own property. "year" does not exist. "toString" is inherited from `Object.prototype`.
Checking for array indices
const fruits = ["apple", "banana"];
console.log(0 in fruits);
console.log(1 in fruits);
console.log(2 in fruits); // Index 2 does not existtrue true false
The `in` operator can check if an index exists in an array.
Distinguishing from `undefined` check
const obj = { a: undefined, b: 10 };
console.log("a" in obj); // true, property 'a' exists but its value is undefined
console.log(obj.a === undefined); // true
console.log("c" in obj); // false, property 'c' does not exist
console.log(obj.c === undefined); // truetrue true false true
`in` operator correctly identifies that 'a' exists, even if its value is `undefined`. `obj.c === undefined` is `true` for both non-existent and `undefined` properties, making `in` more precise for existence checks.
Debug faster
Common Errors
Confusing `in` with `hasOwnProperty()`
Cause: Using `in` when you only want to check for properties directly on the object, not inherited ones.
Fix: If you only care about "own" properties (not from the prototype chain), use `Object.prototype.hasOwnProperty.call(obj, propName)` or `obj.hasOwnProperty(propName)` (though the former is safer for objects without a clean prototype chain).
const obj = {};
console.log("toString" in obj); // true (inherited)
console.log(obj.hasOwnProperty("toString")); // false (not own property)Runtime support
Compatibility
Source: MDN Web Docs
Common questions
Frequently Asked Questions
Returns `true` if the specified property is in the specified object or its prototype chain, otherwise returns `false`.
Confusing `in` with `hasOwnProperty()`: If you only care about "own" properties (not from the prototype chain), use `Object.prototype.hasOwnProperty.call(obj, propName)` or `obj.hasOwnProperty(propName)` (though the former is safer for objects without a clean prototype chain).