How to Check if an Object Has a Property in JavaScript

Checking if an object has a specific property is crucial for defensive programming and avoiding runtime errors when accessing object properties. As the creator of CoreUI with over 25 years of JavaScript development experience, I regularly validate object properties when processing API responses and user configurations. The most reliable method is using Object.prototype.hasOwnProperty() or the modern Object.hasOwn() method, which check for own properties without inherited ones.

Use Object.hasOwn() or hasOwnProperty() to safely check if an object has a specific property without checking the prototype chain.

const user = { name: 'John', age: 30, city: 'New York' }

// Modern approach (ES2022+)
console.log(Object.hasOwn(user, 'name')) // true
console.log(Object.hasOwn(user, 'email')) // false

// Traditional approach
console.log(user.hasOwnProperty('name')) // true
console.log(user.hasOwnProperty('email')) // false

// Using 'in' operator (checks prototype chain too)
console.log('name' in user) // true
console.log('toString' in user) // true (inherited from Object.prototype)

// Checking for undefined (less reliable)
console.log(user.name !== undefined) // true
console.log(user.email !== undefined) // false

Object.hasOwn() is the modern standard that safely checks for own properties without prototype chain interference. The hasOwnProperty() method works similarly but can be overridden. The in operator checks both own and inherited properties. Avoid using !== undefined checks alone as properties can legitimately have undefined values. Use bracket notation for dynamic property names like Object.hasOwn(obj, variableName).

Best Practice Note:

In CoreUI components, we consistently use Object.hasOwn() when validating component props and configuration objects. This prevents errors when users pass incomplete configuration and ensures our components gracefully handle missing properties with appropriate defaults.


Speed up your responsive apps and websites with fully-featured, ready-to-use open-source admin panel templates—free to use and built for efficiency.


About the Author

Subscribe to our newsletter
Get early information about new products, product updates and blog posts.
How to Use Bootstrap Dropdown in Vue 3 – CoreUI Integration Guide
How to Use Bootstrap Dropdown in Vue 3 – CoreUI Integration Guide

How to Remove Underline from Link in CSS
How to Remove Underline from Link in CSS

How to Merge Objects in JavaScript
How to Merge Objects in JavaScript

How to remove a property from an object in Javascript
How to remove a property from an object in Javascript

Answers by CoreUI Core Team