Full answer
There are three common ways to ask "does this object have this property?", and they answer different questions.
'key' in obj: true if the property exists on the object or anywhere on its prototype chain, whether or not the value is undefined. So 'toString' in {} is true, because toString is inherited from Object.prototype. It also works with arrays (indexes) and with symbols. It throws a TypeError if the right side is not an object.
Object.hasOwn(obj, 'key'): true only for an own property, including one whose value is undefined. It is the modern replacement for obj.hasOwnProperty('key'). The old form has two problems: it fails on objects with a null prototype (they have no such method), and it breaks if the object has its own property named hasOwnProperty. Object.hasOwn works for both.
obj.key !== undefined (or obj.key == null checks): this reads the value, so it cannot tell a missing property from a property that holds undefined. It also sees inherited values. Use it when you only care whether there is a usable value, often with ?? or ?.: const port = config.port ?? 3000.
Which to choose:
- Dictionary or parsed JSON data, where inherited keys must not count:
Object.hasOwn. - Checking for a method or a feature, such as
'fetch' in globalThis, or'then' in valueto detect a thenable:in. - Default values:
??or optional chaining.
Related details: for...in loops over own and inherited enumerable string keys, while Object.keys returns own enumerable keys only. For Map, use map.has(key), which is also the safe choice for user-provided keys. Using plain objects as dictionaries with user keys like __proto__ or constructor is a security risk, so use Map or Object.create(null).
const base = { inherited: 1 };
const obj = Object.create(base);
obj.own = 'value';
obj.empty = undefined;
for (const key of ['own', 'empty', 'inherited', 'missing', 'toString']) {
console.log(
key.padEnd(10),
'in:', String(key in obj).padEnd(5),
'hasOwn:', String(Object.hasOwn(obj, key)).padEnd(5),
'!== undefined:', obj[key] !== undefined,
);
}
const plain = { hasOwnProperty: 'oops' };
try {
plain.hasOwnProperty('x');
} catch (error) {
console.log('old hasOwnProperty style breaks:', error.name);
}
console.log('Object.hasOwn still works:', Object.hasOwn(plain, 'hasOwnProperty'));
const bare = Object.create(null);
bare.key = 1;
console.log('null prototype object:', typeof bare.hasOwnProperty, Object.hasOwn(bare, 'key'), 'toString' in bare);
console.log('arrays:', 1 in ['a', 'b'], 5 in ['a', 'b'], 'length' in [], Object.hasOwn([], 'length'));
try {
'a' in 'abc';
} catch (error) {
console.log('in on a string:', error.name);
}
const config = { port: 0, host: null, debug: false };
console.log('|| loses valid falsy values:', config.port || 3000, '| ?? keeps them:', config.port ?? 3000, '| null host falls back:', config.host ?? 'localhost');
console.log('optional chaining:', config.db?.url, config.db?.url ?? 'no db');
const thenable = { then() {} };
console.log('detect a thenable with in:', 'then' in thenable, 'then' in {});
const dictionary = JSON.parse('{"__proto__": 1, "constructor": 2}');
console.log(Object.keys(dictionary), Object.hasOwn(dictionary, '__proto__'), new Map(Object.entries(dictionary)).has('constructor'));Run with Node.js v24.22.1 as js-159.js; two runs gave identical output. Output: own in: true hasOwn: true !== undefined: true empty in: true hasOwn: true !== undefined: false inherited in: true hasOwn: false !== undefined: true missing in: false hasOwn: false !== undefined: false toString in: true hasOwn: false !== undefined: true old hasOwnProperty style breaks: TypeError Object.hasOwn still works: true null prototype object: undefined true false arrays: true false true true in on a string: TypeError || loses valid falsy values: 3000 | ?? keeps them: 0 | null host falls back: localhost optional chaining: undefined no db detect a thenable with in: true false [ '__proto__', 'constructor' ] true true
Key points
- in checks own and inherited properties, including those with undefined values.
- Object.hasOwn checks own properties only and works for null-prototype objects.
- value !== undefined cannot tell a missing property from a property set to undefined.
- Prefer hasOwn for data objects and in for feature detection; use ?? and ?. for defaults.
- Use Map for dictionaries with user-provided keys.
Common mistakes
- Using obj.hasOwnProperty(key) on data from outside, which may not be a normal object.
- Using in when only own keys should count.
- Using || for defaults and losing 0, empty strings, and false.
- Checking value !== undefined and thinking it proves the key is missing.
Follow-ups
- Why does the old hasOwnProperty call fail on Object.create(null) objects?
- What is the difference between Object.keys and for...in?
- How does a Map avoid the problems of plain objects as dictionaries?
Tips
- Say which question each check answers.
- Recommend Object.hasOwn and explain why it replaced hasOwnProperty.
- Mention Map for user-supplied keys.