in
Checks whether an object has a property (inherited ones included).
Available since: ES1
Syntax
key in objectExamples
Does the property exist:
const user = { name: 'Anna', age: undefined };
console.log('name' in user);
console.log('email' in user);
console.log('age' in user);Output:
true
false
trueInherited properties count too:
console.log('toString' in {});
console.log(Object.hasOwn({}, 'toString'));Output:
true
falseNotes
Do not confuse it with the for...in loop — that is a different construct. For arrays in checks indexes, not values: 2 in ['a', 'b', 'c'] is true. Search array values with includes().
See also
- Object.hasOwn — Checks whether an object has its own property.
- Optional chaining (?.) — Safe property access: returns undefined instead of an error if the object is missing.
- for...in — Loops over the property names of an object.
In the tutorial
- Objects — Objects in JavaScript: properties, dot and bracket access, adding, deleting and nested data.