Property presence in an object
JavaScript has several ways to check whether an object has a given property. Each one works a little differently: let's go through all of them, with examples and nuances.
1. The in operator
The most direct and reliable approach.
const user = { name: 'Oleh', age: 25 };
console.log('name' in user); // true
console.log('email' in user); // falseChecks whether the property exists at all (including ones inherited from the prototype).
Example with inheritance
const person = { isHuman: true };
const user = Object.create(person);
user.name = 'Oleh';
console.log('name' in user); // true (own)
console.log('isHuman' in user); // true (inherited)2. The hasOwnProperty() method
Checks only own properties (that is, ones that actually belong to the object itself, not the prototype).
const person = { isHuman: true };
const user = Object.create(person);
user.name = 'Oleh';
console.log(user.hasOwnProperty('name')); // true
console.log(user.hasOwnProperty('isHuman')); // falseUse it when you need to make sure a property is defined on the object itself, not inherited.
Be careful:
If someone has overridden hasOwnProperty, it is better to call it like this:
Object.prototype.hasOwnProperty.call(user, 'name');3. Checking against undefined
A simple, but not always safe, approach:
const user = { name: 'Oleh' };
console.log(user.name !== undefined); // true
console.log(user.email !== undefined); // falseThe problem: a property can exist but have the value
undefined.
const user = { name: undefined };
console.log('name' in user); // true
console.log(user.name !== undefined); // false (misleading)4. The Object.hasOwn() method (modern, ES2022)
This is a "new version" of hasOwnProperty,
but safer (it does not depend on the prototype chain).
const user = { name: 'Oleh' };
console.log(Object.hasOwn(user, 'name')); // true
console.log(Object.hasOwn(user, 'email')); // falseRecommended over
hasOwnProperty()in modern code.
Comparing the approaches
| Method | Checks inherited properties | Checks only its own | Safe for any object | Example |
|---|---|---|---|---|
'key' in obj | Yes | No | Yes | 'name' in user |
obj.hasOwnProperty('key') | No | Yes | Not always (can be overridden) | user.hasOwnProperty('name') |
Object.hasOwn(obj, 'key') | No | Yes | Yes | Object.hasOwn(user, 'name') |
obj.key !== undefined | Not always | No | Yes | user.name !== undefined |
5. Checking via Reflect.has() (an alternative to in)
Another option (from the Reflect API):
const user = { name: 'Oleh' };
console.log(Reflect.has(user, 'name')); // true
console.log(Reflect.has(user, 'email')); // falseWorks exactly like
in, but is convenient in "reflective" (metaprogramming) code.
Summary
| Goal | Best approach |
|---|---|
| Check any property (including from prototypes) | 'prop' in obj |
| Check only its own property | Object.hasOwn(obj, 'prop') |
| Support for older browsers | obj.hasOwnProperty('prop') |
Check that the value is not undefined | obj.key !== undefined (with caution) |
In short
in: checks whether a property exists (including inherited ones).hasOwnProperty(): checks only own properties.Object.hasOwn(): a modern and safe alternative.- Checking
!== undefinedis not always correct.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.