Start using Object.hasOwn()

Above - screenshot from MDN’s Object.hasOwn() documentation.
If you so far waited to make the switch because of “lack of wide support”:
Since April 2025 - even the Android native browser and Android Firefox support it by now (and there has been wide support for all major browsers since 2022).
Why use any of them?
You want to check if a property exist directly on an object. The most foolproof way to do that was to call .hasOwnProperty
on the object itself.
Why not just use if (obj.prop)
or (if “prop” in obj)
?
The reason for the first case, is, this: { prop: null }
will return false
even though prop
does exist in the object (same goes for { prop: undefined }
, { prop: false }
or any falsy value.
The reason for the second case is, in
searches for the property through the entire prototype chain - sometimes you want this, but if you intended to check the existence of the property directly on the object, you might encounter undesirable results (maybe the property was defined someplace else on the chain, by you, or a 3rd party?).
Only .hasOwnProperty
was the safest way to check if a property directly exist on an object.
So, what’s wrong with it?
It’s overridable
Either with a simple { hasOwnProperty() { console.log(“Ha Ha!”); } }
or with a more “low-level” Object.defineProperty
Proxy getters will add an overhead
If you Proxy an object and add a custom getter - it will be called every time you’ll call hasOwnProperty
- which will introduce an annoying overhead, and checking if a property exists on an object is a pretty common thing to do in Javascript.
It makes more overall sense, semantically, abstractly
hasOwnPrototype
is a method defined on the prototype
of Object
which all things in Javascript inherit (well… except for null
which only “thinks” it’s an object (actually the reason that typeof null
returns object
is historically interesting, but out of the scope of this article), or undefined
).
But, it kinda makes sense that the action of “checking if a property exists on an object” should be a static method (like Object.keys
or Object.values
and so on).
So… no more excuses
Migrate to Object.hasOwn
today!
Subscribe to my newsletter
Read articles from Yuval Aloni directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Yuval Aloni
Yuval Aloni
Raised in a family of programmers. Started writing Assembly code at the age of 13. Turned to web development at the age of 26. I like creativity and innovation. The combinations between high-level and low-level, between specifics and abstractions, between art and technology. Creator of the DeriveJS ODM for MongoDB: a back-end framework that enables you to use normal JS classes and objects as "data entities" that automatically sync and persist to MongoDB collections and documents, plus an engine that auto bulks your data operations for performance. Creator of SproutJS: https://jssprout.com, a client-side framework that adds reactivity and state management to native HTML elements, using Web Components / Custom Elements.