JavaScriptEasy
How do you check if an object has a specific property?
By FrontendPro Editorial Team Updated 8/8/2026
#JavaScript
Answer
To check if an object has a specific property, you can use the in operator or the hasOwnProperty method. The in operator checks for both own and inherited properties, while hasOwnProperty checks only for own properties.
js
const obj = { key: "value" };
// Using the `in` operator
if ("key" in obj) {
console.log("Property exists");
}
// Using `hasOwnProperty`
if (obj.hasOwnProperty("key")) {
console.log("Property exists");
}
