Ask any question about JavaScript here... and get an instant response.
Post this Question & Answer:
How can I check if an object has a specific property in JavaScript?
Asked on May 24, 2026
Answer
To check if an object has a specific property in JavaScript, you can use the "hasOwnProperty" method or the "in" operator. Both are effective, but they serve slightly different purposes.
const obj = { name: 'Alice', age: 25 };
// Using hasOwnProperty
console.log(obj.hasOwnProperty('name')); // true
// Using the 'in' operator
console.log('age' in obj); // trueAdditional Comment:
✅ Answered with JavaScript best practices.- "hasOwnProperty" checks if the property is a direct property of the object, not inherited through the prototype chain.
- The "in" operator checks if the property exists in the object or its prototype chain.
- Use "hasOwnProperty" when you want to ensure the property is not inherited.
- Use "in" when you want to check for both own and inherited properties.
Recommended Links:
