File size: 6,345 Bytes
780c9fe | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | ---
title: Object.prototype.hasOwnProperty()
short-title: hasOwnProperty()
slug: Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty
page-type: javascript-instance-method
browser-compat: javascript.builtins.Object.hasOwnProperty
sidebar: jsref
---
The **`hasOwnProperty()`** method of {{jsxref("Object")}} instances returns a boolean indicating whether this
object has the specified property as its own property (as opposed to inheriting
it).
> [!NOTE]
> {{jsxref("Object.hasOwn()")}} is recommended over
> `hasOwnProperty()`, in browsers where it is supported.
{{InteractiveExample("JavaScript Demo: Object.prototype.hasOwnProperty()")}}
```js interactive-example
const object = {};
object.foo = 42;
console.log(object.hasOwnProperty("foo"));
// Expected output: true
console.log(object.hasOwnProperty("toString"));
// Expected output: false
console.log(object.hasOwnProperty("hasOwnProperty"));
// Expected output: false
```
## Syntax
```js-nolint
hasOwnProperty(prop)
```
### Parameters
- `prop`
- : The {{jsxref("String")}} name or [Symbol](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol) of the property to test.
### Return value
Returns `true` if the object has the specified property as own property; `false`
otherwise.
## Description
The **`hasOwnProperty()`** method returns `true` if the specified property is a
direct property of the object — even if the value is `null` or `undefined`. The
method returns `false` if the property is inherited, or has not been declared at
all. Unlike the {{jsxref("Operators/in", "in")}} operator, this
method does not check for the specified property in the object's prototype
chain.
The method can be called on _most_ JavaScript objects, because most objects
descend from {{jsxref("Object")}}, and hence inherit its methods. For
example {{jsxref("Array")}} is an {{jsxref("Object")}}, so you can
use `hasOwnProperty()` method to check whether an index exists:
```js
const fruits = ["Apple", "Banana", "Watermelon", "Orange"];
fruits.hasOwnProperty(3); // true ('Orange')
fruits.hasOwnProperty(4); // false - not defined
```
The method will not be available in objects where it is reimplemented, or on
[`null`-prototype objects](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object#null-prototype_objects) (as these don't inherit from
`Object.prototype`). Examples for these cases are given below.
## Examples
### Using hasOwnProperty to test for an own property's existence
The following code shows how to determine whether the `example` object contains a property named `prop`.
```js
const example = {};
example.hasOwnProperty("prop"); // false
example.prop = "exists";
example.hasOwnProperty("prop"); // true - 'prop' has been defined
example.prop = null;
example.hasOwnProperty("prop"); // true - own property exists with value of null
example.prop = undefined;
example.hasOwnProperty("prop"); // true - own property exists with value of undefined
```
### Direct vs. inherited properties
The following example differentiates between direct properties and properties inherited through the prototype chain:
```js
const example = {};
example.prop = "exists";
// `hasOwnProperty` will only return true for direct properties:
example.hasOwnProperty("prop"); // true
example.hasOwnProperty("toString"); // false
example.hasOwnProperty("hasOwnProperty"); // false
// The `in` operator will return true for direct or inherited properties:
"prop" in example; // true
"toString" in example; // true
"hasOwnProperty" in example; // true
```
### Iterating over the properties of an object
The following example shows how to iterate over the enumerable properties of an
object without executing on inherited properties.
```js
const buz = {
fog: "stack",
};
for (const name in buz) {
if (buz.hasOwnProperty(name)) {
console.log(`this is fog (${name}) for sure. Value: ${buz[name]}`);
} else {
console.log(name); // toString or something else
}
}
```
Note that the {{jsxref("Statements/for...in", "for...in")}} loop
only iterates enumerable items: the absence of non-enumerable properties emitted
from the loop does not imply that `hasOwnProperty` itself is confined strictly
to enumerable items. You can iterate over non-enumerable properties with
{{jsxref("Object.getOwnPropertyNames()")}}.
### Using hasOwnProperty as a property name
JavaScript does not protect the property name `hasOwnProperty`; an object that
has a property with this name may return incorrect results:
```js
const foo = {
hasOwnProperty() {
return false;
},
bar: "Here be dragons",
};
foo.hasOwnProperty("bar"); // re-implementation always returns false
```
The recommended way to overcome this problem is to instead use
{{jsxref("Object.hasOwn()")}} (in browsers that support it). Other
alternatives include using an _external_ `hasOwnProperty`:
```js
const foo = { bar: "Here be dragons" };
// Use Object.hasOwn() method - recommended
Object.hasOwn(foo, "bar"); // true
// Use the hasOwnProperty property from the Object prototype
Object.prototype.hasOwnProperty.call(foo, "bar"); // true
// Use another Object's hasOwnProperty
// and call it with 'this' set to foo
({}).hasOwnProperty.call(foo, "bar"); // true
```
Note that in the first two cases there are no newly created objects.
### Objects created with Object.create(null)
[`null`-prototype objects](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object#null-prototype_objects) do not
inherit from `Object.prototype`, making `hasOwnProperty()` inaccessible.
```js
const foo = Object.create(null);
foo.prop = "exists";
foo.hasOwnProperty("prop"); // Uncaught TypeError: foo.hasOwnProperty is not a function
```
The solutions in this case are the same as for the previous section: use
{{jsxref("Object.hasOwn()")}} by preference, otherwise use an
external object's `hasOwnProperty()`.
## Specifications
{{Specifications}}
## Browser compatibility
{{Compat}}
## See also
- {{jsxref("Object.hasOwn()")}}
- [Enumerability and ownership of properties](/en-US/docs/Web/JavaScript/Guide/Enumerability_and_ownership_of_properties)
- {{jsxref("Object.getOwnPropertyNames()")}}
- {{jsxref("Statements/for...in", "for...in")}}
- {{jsxref("Operators/in", "in")}}
- [Inheritance and the prototype chain](/en-US/docs/Web/JavaScript/Guide/Inheritance_and_the_prototype_chain)
|