Check if a Key Exists in JavaScript: Own vs Inherited Properties
- Authors

- Name
- Geeks Kai
- @KaiGeeks
Use Object.hasOwn(record, key) when a property must belong to the object itself. Use key in record or Reflect.has(record, key) when inherited properties count. A property whose value is undefined, null, false, 0 or an empty string can still exist.
const exampleRecord = Object.create({ inherited: true })
exampleRecord.optional = undefined
exampleRecord.empty = null
Object.defineProperty(exampleRecord, "hidden", { value: 7, enumerable: false })
const symbolKey = Symbol("id")
exampleRecord[symbolKey] = "record-1"
console.log(Object.hasOwn(exampleRecord, "optional")) // true
console.log(exampleRecord.optional !== undefined) // false: this is a value check
console.log(Object.hasOwn(exampleRecord, "empty")) // true
console.log(Object.hasOwn(exampleRecord, "inherited")) // false
console.log("inherited" in exampleRecord) // true
console.log(Reflect.has(exampleRecord, "inherited")) // true
console.log(Object.hasOwn(exampleRecord, "hidden")) // true
console.log(Object.keys(exampleRecord).includes("hidden")) // false
console.log(Object.hasOwn(exampleRecord, symbolKey)) // true
| Requirement | Check | Boundary |
|---|---|---|
| Own property, including non-enumerable or Symbol keys | Object.hasOwn(record, key) | Does not search the prototype chain |
| Own or inherited property | key in record / Reflect.has(record, key) | Includes prototype properties |
| Value is not undefined | record[key] !== undefined | Missing and present-with-undefined both fail |
| Own enumerable string keys | Object.keys(record) | Excludes non-enumerable and Symbol keys |
| Any own key during inspection | Reflect.ownKeys(record) | Includes strings and Symbols |
Choose by meaning, not a claimed universal speed ranking. Object.entries() is useful when you already need enumerable string key/value pairs, but building an array is unnecessary for a single own-key question.
Calling record.hasOwnProperty(key) assumes that method exists and has not been replaced. The static Object.hasOwn() avoids that assumption. For environments without it, Object.prototype.hasOwnProperty.call(record, key) is the established own-property alternative.
const dictionary = Object.create(null)
dictionary.name = "Kai"
console.log(Object.hasOwn(dictionary, "name")) // true
const shadowedRecord = { hasOwnProperty: false, name: "Kai" }
console.log(Object.hasOwn(shadowedRecord, "name")) // true
console.log(Object.prototype.hasOwnProperty.call(shadowedRecord, "name")) // true
function hasOwnKey(record, key) {
if (record === null || record === undefined) {
return false
}
return Object.hasOwn(record, key)
}
console.log(hasOwnKey(null, "name")) // false
console.log(hasOwnKey({ name: undefined }, "name")) // true
The helper above explicitly returns false for a nullish container. It otherwise delegates to the native method; it is not complete schema validation. Native Object.hasOwn(null, key) and Object.hasOwn(undefined, key) throw, while other primitives can be boxed. Decide your input contract rather than silently converting unknown input.
record?.optional safely accesses a nullish container but returns undefined both when a property is missing and when its value is undefined. record.optional ?? fallback chooses a fallback for either null or undefined; it does not tell you whether the key exists. Use an own-key check separately when presence matters.
Object.getOwnPropertyDescriptors(record) returns an ordinary object of descriptors. Testing "toString" in descriptors can find an inherited property even when the original record has no own toString key. Use Object.hasOwn(descriptors, key) or query the original object directly.
isPrototypeOf() answers whether one object appears in another object’s prototype chain. It is not a property-existence check. Also remember that a presence check does not validate a value, make a getter safe to execute, or sanitize untrusted input.
Yes. The optional property in the example is an own property even though reading it produces undefined. This is why a truthiness or value comparison is not interchangeable with a presence check.
Yes. Pass the actual Symbol value. A string with the same description is a different key. Object.keys() and Object.getOwnPropertyNames() do not enumerate Symbol keys.
First validate or guard each container, then check the required key at that level. Optional chaining helps safe access; it does not replace the decision about own versus inherited properties.