JavaScript (ES2026) reference · Object Methods
JavaScript Object.hasOwn() Method
Return true if an object has a property of its own (not inherited). The safer replacement for hasOwnProperty().
JavaScript (ES2026) Since ES2022
Syntax
Object.hasOwn(obj, prop)
Parameters
| Parameter | Description |
|---|---|
obj |
The object to inspect or change. |
prop |
The property name. |
Return value
true or false.
Examples
const user = { name: "Ada" };
console.log(Object.hasOwn(user, "name"), Object.hasOwn(user, "toString"), "toString" in user);
Output
true false true
const dict = Object.create(null);
dict.key = 1;
console.log(Object.hasOwn(dict, "key"));
// dict.hasOwnProperty("key") would throw: there is no prototype
Output
true
Version history
| Version | Change |
|---|---|
| JavaScript ES2022 | Added |
In other languages
array_key_exists()· PHP
Related
Object.keys()· Return an array of an object's own enumerable property names, in insertion order (integer keys first).Object.create()· Create a new object with the given prototype (or null for an object with no inherited properties at all).- JavaScript Objects · tutorial