Skip to main content

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.

javascript
const user = { name: 'Oleh', age: 25 }; console.log('name' in user); // true console.log('email' in user); // false

Checks whether the property exists at all (including ones inherited from the prototype).


Example with inheritance

javascript
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).

javascript
const person = { isHuman: true }; const user = Object.create(person); user.name = 'Oleh'; console.log(user.hasOwnProperty('name')); // true console.log(user.hasOwnProperty('isHuman')); // false

Use 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:

javascript
Object.prototype.hasOwnProperty.call(user, 'name');

3. Checking against undefined

A simple, but not always safe, approach:

javascript
const user = { name: 'Oleh' }; console.log(user.name !== undefined); // true console.log(user.email !== undefined); // false

The problem: a property can exist but have the value undefined.

javascript
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).

javascript
const user = { name: 'Oleh' }; console.log(Object.hasOwn(user, 'name')); // true console.log(Object.hasOwn(user, 'email')); // false

Recommended over hasOwnProperty() in modern code.


Comparing the approaches

MethodChecks inherited propertiesChecks only its ownSafe for any objectExample
'key' in objYesNoYes'name' in user
obj.hasOwnProperty('key')NoYesNot always (can be overridden)user.hasOwnProperty('name')
Object.hasOwn(obj, 'key')NoYesYesObject.hasOwn(user, 'name')
obj.key !== undefinedNot alwaysNoYesuser.name !== undefined

5. Checking via Reflect.has() (an alternative to in)

Another option (from the Reflect API):

javascript
const user = { name: 'Oleh' }; console.log(Reflect.has(user, 'name')); // true console.log(Reflect.has(user, 'email')); // false

Works exactly like in, but is convenient in "reflective" (metaprogramming) code.


Summary

GoalBest approach
Check any property (including from prototypes)'prop' in obj
Check only its own propertyObject.hasOwn(obj, 'prop')
Support for older browsersobj.hasOwnProperty('prop')
Check that the value is not undefinedobj.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 !== undefined is not always correct.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.