How do you check if a property exists?
By FrontendPro Editorial Team Updated 9/26/2026
Answer
There are four common ways, and they answer slightly different questions.
const obj = { a: 1, b: undefined };
"a" in obj; // true — own or inherited
Object.hasOwn(obj, "a"); // true — own only
obj.hasOwnProperty("a"); // true — own only, older form
obj.a !== undefined; // true — but see below
The difference that matters
in walks the prototype chain, so it reports inherited properties too:
"toString" in obj; // true — inherited from Object.prototype
Object.hasOwn(obj, "toString"); // false — not the object's own
For a plain data object that distinction rarely matters; for anything with a prototype, or when checking keys from user input, it matters a lot.
Why comparing to undefined is not the same
A property can exist and hold undefined:
obj.b !== undefined; // false — but the key IS there
Object.hasOwn(obj, "b"); // true
That difference decides whether { b: undefined } means "not provided" or "explicitly cleared" — for example when building a partial update.
Prefer Object.hasOwn
It replaces hasOwnProperty and avoids two of its problems: an object can shadow the method with its own hasOwnProperty key, and objects made with Object.create(null) do not have it at all. The old workaround was Object.prototype.hasOwnProperty.call(obj, key) — Object.hasOwn(obj, key) says the same thing plainly.
Related tools
Optional chaining reads safely without checking first: obj?.a?.b. And ?? distinguishes "missing or null" from falsy values, so obj.count ?? 10 keeps a legitimate 0 that obj.count || 10 would discard.
