How to check if a property exists in an object?

👨‍💻 Frontend Developer 🟠 May come up 🎚️ Easy
#JavaScript #Objects #JS Basics

Brief Answer

Check property existence in an object can be done with three main ways: [in] operator, hasOwnProperty method, or comparison with undefined value. Each way has its own features and is used in different situations.


Full Answer

Checking property existence is an important operation in JavaScript, especially when working with dynamic objects. There are several simple ways to do this.

Main Checking Methods

1. In Operator

Checks property existence in object and its prototypes:

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

2. hasOwnProperty Method

Checks only object’s own properties:

const user = { name: 'John' };
console.log(user.hasOwnProperty('name')); // true
console.log(user.hasOwnProperty('toString')); // false

3. Comparison with undefined

Simple way, but with pitfalls:

const user = { name: 'John', age: undefined };
console.log(user.name !== undefined); // true
console.log(user.age !== undefined); // false (though property exists!)

When to Use What

In Operator

// When need to check any property, including inherited
if ('toString' in user) {
  // toString exists for all objects
}

hasOwnProperty

// When need to check only own properties
if (user.hasOwnProperty('name')) {
  // Check only what we added
}

Comparison with undefined

// Simple case, but careful with undefined values
if (user.name !== undefined) {
  // Works, but can deceive
}

Simple Examples

Check Before Use

const user = { name: 'John' };
 
// Good - check before use
if ('age' in user) {
  console.log(user.age);
} else {
  console.log('Age not specified');
}

Working with Dynamic Keys

const user = { name: 'John', age: 25 };
const field = 'name';
 
if (field in user) {
  console.log(user[field]); // Safe access
}

Important Differences

1. Inheritance

const obj = {};
console.log('toString' in obj); // true (inherited)
console.log(obj.hasOwnProperty('toString')); // false (not own)

2. Undefined Value

const obj = { value: undefined };
console.log('value' in obj); // true (property exists)
console.log(obj.value !== undefined); // false (but value is undefined)

Common Mistakes

1. Using Comparison with undefined

// ❌ Error - doesn't work with undefined values
const obj = { value: undefined };
if (obj.value !== undefined) {
  console.log('Has value');
} else {
  console.log('No value'); // Will trigger, though property exists!
}
 
// ✅ Correct - use in or hasOwnProperty
if ('value' in obj) {
  console.log('Property exists'); // Correct result
}

2. Ignoring Inheritance

// ❌ May give unexpected result
const obj = {};
if ('toString' in obj) {
  console.log('toString exists'); // Always true!
}
 
// ✅ Check only own properties
if (obj.hasOwnProperty('toString')) {
  console.log('toString added by us');
} else {
  console.log('toString inherited'); // Correct result
}

Simple Rules

  1. In operator — for general check, including inheritance
  2. hasOwnProperty — when need only own properties
  3. Comparison with undefined — only for simple cases
  4. Careful with undefined — property can exist but have undefined value
  5. Dynamic keys — use square brackets

Checking property existence helps write reliable code and avoid errors when working with objects.


Want more articles to prepare for interviews? Subscribe to EasyAdvice, bookmark the site and improve yourself every day 💪