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? Pending Review
Asked on May 03, 2026
Answer
To check if an object has a specific property, you can use the "in" operator or the "hasOwnProperty" method. Both methods are effective, but they have slight differences in behavior.
const obj = { name: "Alice", age: 30 };
// Using the "in" operator
console.log("name" in obj); // true
// Using the "hasOwnProperty" method
console.log(obj.hasOwnProperty("age")); // trueAdditional Comment:
✅ Answered with JavaScript best practices.- The "in" operator checks if the property exists in the object or its prototype chain.
- The "hasOwnProperty" method checks only for the object's own properties, not those in the prototype chain.
- Both methods return a boolean value indicating the presence of the property.
Recommended Links:
