Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

Object.prototype.hasOwnProperty

Checks whether an object has its own property (the older way).

Available since: ES3

Syntax

obj.hasOwnProperty(key)

Parameters

ParameterTypeDescription
keystring | symbolThe 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
false

Notes

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

Thanks for reading!

Donate to the site — we work hard for you.