hasOwnProperty()
ES3+Returns a boolean indicating whether the object has the specified property as its own property.
Syntax
obj.hasOwnProperty(prop)Parameters
prop string The property name to check
Return Value
true if the object has the property, otherwise false
Examples
const obj = { name: 'John' };
console.log(obj.hasOwnProperty('name'));
console.log(obj.hasOwnProperty('age'));
console.log(obj.hasOwnProperty('toString')); 📌 When to Use
Use hasOwnProperty() when you need to check if an object has a specific property as its own (not inherited) property. Essential for safe property checking in for...in loops, validating user input against object schemas, or distinguishing own properties from prototype properties.
⚠️ Common Mistakes
Calling hasOwnProperty directly on objects that might have it overridden - use Object.prototype.hasOwnProperty.call(obj, prop) for safety.
Using hasOwnProperty on objects created with Object.create(null) - they do not have this method, use Object.hasOwn() or the call pattern.
Confusing hasOwnProperty with the "in" operator - "in" checks the entire prototype chain, hasOwnProperty only checks own properties.
✅ Best Practices
Use Object.hasOwn(obj, prop) in modern JavaScript (ES2022+) - it is safer and more concise than hasOwnProperty.
In for...in loops, always check hasOwnProperty to filter out inherited properties: if (obj.hasOwnProperty(key)) { ... }
For untrusted objects, use the safe pattern: Object.prototype.hasOwnProperty.call(obj, prop) or the shorthand ({}).hasOwnProperty.call(obj, prop)
⚡ Performance Notes
hasOwnProperty() is an O(1) operation for simple property lookup. It does not traverse the prototype chain. The Object.prototype.hasOwnProperty.call() pattern has minimal overhead. Object.hasOwn() (ES2022+) is slightly faster in modern engines as it is optimized for this specific use case.
🌍 Real World Example
Safe Object Property Iteration
Safely iterate over object properties while filtering out inherited properties using different hasOwnProperty patterns.
// Base object with inherited method
const proto = { inherited: 'from prototype' };
const obj = Object.create(proto);
obj.own1 = 'value1';
obj.own2 = 'value2';
// Method 1: Direct hasOwnProperty (may be overridden)
console.log(obj.hasOwnProperty('own1')); // true
console.log(obj.hasOwnProperty('inherited')); // false
// Method 2: Safe pattern (recommended for untrusted objects)
const hasOwn = Object.prototype.hasOwnProperty;
console.log(hasOwn.call(obj, 'own1')); // true
// Method 3: Modern Object.hasOwn (ES2022+)
console.log(Object.hasOwn(obj, 'own1')); // true
console.log(Object.hasOwn(obj, 'inherited')); // false
// Safe for...in iteration
function getOwnProperties(obj) {
const result = {};
for (const key in obj) {
if (Object.hasOwn(obj, key)) {
result[key] = obj[key];
}
}
return result;
}
console.log(getOwnProperties(obj));
// { own1: 'value1', own2: 'value2' }
// Handle edge case: object with overridden hasOwnProperty
const tricky = { hasOwnProperty: () => false, name: 'test' };
console.log(tricky.hasOwnProperty('name')); // false (wrong!)
console.log(Object.hasOwn(tricky, 'name')); // true (correct!)