Suggest an editImprove this articleRefine the answer for “Property presence in an object”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)**`'key' in obj`** checks whether a property exists at all (including ones inherited from the prototype), while **`Object.hasOwn(obj, 'key')`** checks only the object's own properties. **Key point:** checking with `obj.key !== undefined` is not always correct, because a property can exist and still hold the value `undefined`.Shown above the full answer for quick recall.Answer (EN)ImageJavaScript has **several ways to check** whether an object has a given property. Each one works a little differently: let's go through all of them, with examples and nuances. --- ## 1. The `in` operator The most direct and reliable approach. ```javascript const user = { name: 'Oleh', age: 25 }; console.log('name' in user); // true console.log('email' in user); // false ``` > Checks **whether the property exists at all** (including ones inherited from the prototype). --- ### Example with inheritance ```javascript const person = { isHuman: true }; const user = Object.create(person); user.name = 'Oleh'; console.log('name' in user); // true (own) console.log('isHuman' in user); // true (inherited) ``` --- ## 2. The `hasOwnProperty()` method Checks **only own properties** (that is, ones that actually belong to the object itself, not the prototype). ```javascript const person = { isHuman: true }; const user = Object.create(person); user.name = 'Oleh'; console.log(user.hasOwnProperty('name')); // true console.log(user.hasOwnProperty('isHuman')); // false ``` > Use it when you need to make sure a property is defined **on the object itself**, not inherited. --- ### Be careful: If someone has overridden `hasOwnProperty`, it is better to call it like this: ```javascript Object.prototype.hasOwnProperty.call(user, 'name'); ``` --- ## 3. Checking against `undefined` A simple, but not always safe, approach: ```javascript const user = { name: 'Oleh' }; console.log(user.name !== undefined); // true console.log(user.email !== undefined); // false ``` > The problem: a property can exist but have the value `undefined`. ```javascript const user = { name: undefined }; console.log('name' in user); // true console.log(user.name !== undefined); // false (misleading) ``` --- ## 4. The `Object.hasOwn()` method (modern, ES2022) This is a "new version" of `hasOwnProperty`, but safer (it does not depend on the prototype chain). ```javascript const user = { name: 'Oleh' }; console.log(Object.hasOwn(user, 'name')); // true console.log(Object.hasOwn(user, 'email')); // false ``` > Recommended over `hasOwnProperty()` in modern code. --- ## Comparing the approaches | Method | Checks inherited properties | Checks only its own | Safe for any object | Example | |---|---|---|---|---| | `'key' in obj` | Yes | No | Yes | `'name' in user` | | `obj.hasOwnProperty('key')` | No | Yes | Not always (can be overridden) | `user.hasOwnProperty('name')` | | `Object.hasOwn(obj, 'key')` | No | Yes | Yes | `Object.hasOwn(user, 'name')` | | `obj.key !== undefined` | Not always | No | Yes | `user.name !== undefined` | --- ## 5. Checking via `Reflect.has()` (an alternative to `in`) Another option (from the Reflect API): ```javascript const user = { name: 'Oleh' }; console.log(Reflect.has(user, 'name')); // true console.log(Reflect.has(user, 'email')); // false ``` > Works exactly like `in`, but is convenient in "reflective" (metaprogramming) code. --- ## Summary | Goal | Best approach | |---|---| | Check **any** property (including from prototypes) | `'prop' in obj` | | Check **only its own** property | `Object.hasOwn(obj, 'prop')` | | Support for older browsers | `obj.hasOwnProperty('prop')` | | Check that the **value is not** `undefined` | `obj.key !== undefined` (with caution) | --- ## In short - `in`: checks whether a property exists (including inherited ones). - `hasOwnProperty()`: checks only own properties. - `Object.hasOwn()`: a modern and safe alternative. - Checking `!== undefined` is not always correct.For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.