Object.prototype.hasOwnProperty
Checks whether an object has its own property (the older way).
Available since: ES3
Syntax
obj.hasOwnProperty(key)Parameters
| Parameter | Type | Description |
|---|---|---|
| key | string | symbol | The property name. |
Return value
booleantrue if the property belongs to the object itself.
Examples
An own and an inherited property:
const user = { name: 'Anna' };
console.log(user.hasOwnProperty('name'));
console.log(user.hasOwnProperty('toString'));Output:
true
falseNotes
The method does not exist on objects without a prototype (Object.create(null)) and can be shadowed by a property of the same name. So new code uses Object.hasOwn().
See also
- Object.hasOwn — Checks whether an object has its own property.
- in — Checks whether an object has a property (inherited ones included).
In the tutorial
- Iterating objects and Object functions — How to iterate over an object in JavaScript: Object.keys, Object.values, Object.entries, Object.fromEntries, for...in and Object.hasOwn.