geekskai Logogeekskai
ToolsBlogPricing
Sign in
Sign in
ToolsBlogSign in
Monday, September 23, 2024|3.305Mins Read

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

Authors
  • avatar
    Name
    Geeks Kai
    Twitter
    @KaiGeeks
Discuss on Twitter • View on GitHub

Tags

javascriptobjectsproperty-checkingprogramming

Previous Article

Rename TypeScript Fields: Source Refactoring, Mapped Types and JSDoc

Next Article

Cloudflare Pages 搭建博客 | 2026 最新版
← Back to the blog
geekskai Logo
geekskai

Public tools for developers and creators, plus optional Geekskai Audio Toolkit plans. Built with care.

Popular Tools

SoundCloud DownloaderSoundCloud to MP3SoundCloud to WAVSoundCloud PlaylistSoundCloud Artwork
View All Tools

Resources

BlogAbout MePricingTagsProjectsPrivacy PolicyTerms of ServiceRefund Policy

Connect With Us

mailMail
githubGitHub
twitterTwitter
linkedinLinkedin
© 2026 geekskai • All rights reserved

Quick answer: own property or inherited property?

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

Choose the condition before the method

RequirementCheckBoundary
Own property, including non-enumerable or Symbol keysObject.hasOwn(record, key)Does not search the prototype chain
Own or inherited propertykey in record / Reflect.has(record, key)Includes prototype properties
Value is not undefinedrecord[key] !== undefinedMissing and present-with-undefined both fail
Own enumerable string keysObject.keys(record)Excludes non-enumerable and Symbol keys
Any own key during inspectionReflect.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.

Null prototypes, overridden methods and nullable input

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.

Why optional chaining and default values do not answer this question

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.

Two inspection traps

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.

Frequently asked questions

Can a key exist with an undefined value?

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.

Does Object.hasOwn support Symbol keys?

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.

Which method should I use for nested data?

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.

References

  • ECMAScript: Object.hasOwn and object operations
  • MDN: Object.hasOwn examples and input behavior