The in operator in JavaScript answers a precise question: does an object have a property with this name, either directly or through its prototype chain? It returns a boolean, even when the property’s value is false, 0, or undefined.
Use the JavaScript in operator when inherited properties should count. When only a property stored directly on the object matters, use Object.hasOwn(). For arrays, the same check concerns index properties, so an empty slot and an element containing undefined can produce different answers.
Start with a property name
The JavaScript in operator takes a property name on the left and an object on the right. The following example can run in a browser console or Node.js:
const device = { name: "sensor", active: false };
console.log("name" in device); // true
console.log("missing" in device); // false
The result describes the presence of the named property. Here, name exists and missing does not. A variable can supply the name too: const key = "name"; key in device evaluates to true. This is handy when the property to inspect comes from an argument or a configuration choice. JavaScript also lets the in operator check a symbol key: create a symbol, use it as a computed property name, and pass that same symbol on the left.
With JavaScript’s in operator, the left operand is converted to a property key: symbols remain symbols, while other values become strings. For example, 1 in ["a", "b"] checks the property named "1". Names are case-sensitive, so "Name" in device is false. MDN’s operator reference describes the syntax and conversion rules.
Presence is different from truthiness
A condition such as if (device.active) evaluates the value. The JavaScript in operator instead asks whether the property exists. That distinction matters for legitimate settings such as a disabled feature or a count of zero:
const settings = { enabled: false, retries: 0, note: undefined };
console.log("enabled" in settings); // true
console.log(Boolean(settings.enabled)); // false
console.log("retries" in settings); // true
console.log("note" in settings); // true
console.log(settings.missing); // undefined
The in property check in JavaScript distinguishes the existing note from the absent missing, although reading either value gives undefined. If a caller explicitly supplies note: undefined, that still creates a property. This distinction can matter when a configuration format treats an omitted field differently from a supplied field.
Assigning undefined does not remove a property. For an ordinary configurable property, delete settings.note removes it; a subsequent presence check returns false provided no property with that name exists on the prototype chain. Read the value separately when the application needs to decide what the value means. The in operator in a JavaScript condition therefore lets a caller provide a false value without having it mistaken for an omitted setting.
Follow the prototype chain
Objects can inherit properties from other objects. The JavaScript in operator follows that chain, so a positive result does not establish that the property belongs directly to the object being inspected:
const defaults = { theme: "light" };
const user = Object.create(defaults);
user.name = "Avery";
console.log("theme" in user); // true
console.log(Object.hasOwn(user, "theme")); // false
console.log(Object.hasOwn(user, "name")); // true
Here the JavaScript in check finds theme on defaults, the prototype of user. The user object has its own name, but inherits theme. If user.theme = "dark" is added, the new own property shadows the inherited one; both checks for theme then return true.
The Object.hasOwn reference explains that it checks direct properties, including those whose values are null or undefined. It also works with objects created using Object.create(null), which have no inherited hasOwnProperty method. For a dictionary of user-provided names, this direct check helps avoid treating an inherited name such as toString as one of the stored entries.
A JavaScript property test with in is appropriate when inherited behavior is part of the question. For example, checking whether an object exposes a method can intentionally include a method on its prototype. If the next step is to call it, also test that the resulting value is a function; presence alone does not establish its type. A JavaScript property check with in can find non-enumerable properties as well, so visibility in a loop is a separate question.
Arrays: indexes, holes, and values
Arrays are objects, and their indexes are property names. The JavaScript in operator checks whether an index property exists, including through the prototype chain. It does not search the array’s values:
const colors = ["red", "blue"];
console.log(1 in colors); // true
console.log("blue" in colors); // false
console.log(colors.includes("blue")); // true
In this JavaScript array, the in operator finds index 1; includes() answers the separate question of whether "blue" is a value. An array’s length sets the range of possible indexes but does not mean every position has a property.
const slots = new Array(3);
slots[1] = undefined;
console.log(slots.length); // 3
console.log(0 in slots); // false
console.log(1 in slots); // true
console.log(slots[0]); // undefined
console.log(slots[1]); // undefined
A JavaScript in test reveals that index 0 is a hole while index 1 is present. Both reads yield undefined in this example. Deleting an existing element with delete slots[1] creates another hole without shortening the array; assigning undefined keeps the index property. An in operator test in JavaScript is sensitive to the structure of the array, which makes it useful when preserving sparse data matters.
Inherited indexes can change the result. This self-contained example gives one array its own prototype without changing the shared Array.prototype:
const items = new Array(2);
const proto = Object.create(Array.prototype);
proto[0] = "inherited";
Object.setPrototypeOf(items, proto);
console.log(0 in items); // true
console.log(Object.hasOwn(items, 0)); // false
console.log(items[0]); // "inherited"
The in operator for JavaScript arrays sees the inherited index, even though the array has no own element there. If you need to identify an actual own slot, use Object.hasOwn(items, 0). This matters when distinguishing data assigned to a particular array from properties visible through its prototype. An in operator check in JavaScript will also find named array properties such as length, because arrays are objects with properties beyond their indexed elements.
Give the right operand an object
A JavaScript in expression throws a TypeError when its right operand is a primitive, including null or undefined. For uncertain input, guard before checking:
function hasId(value) {
return value !== null &&
(typeof value === "object" || typeof value === "function") &&
"id" in value;
}
console.log(hasId(null)); // false
console.log(hasId({ id: 0 })); // true
For a substring in a string, use the string’s includes() method instead. The JavaScript operator in is intended for property existence on an object, and its right-hand operand must be an object.
Choose the JavaScript in operator when a property inherited through the prototype chain should count; choose Object.hasOwn() when the check must be limited to properties directly on the object.
