# How to check if a property exists in an object in JavaScript

By Atta Ur Rehman Shah (https://attacomsian.com/about). Published Aug 23, 2020, updated Aug 24, 2020. Topics: JavaScript.
Canonical URL: https://attacomsian.com/blog/javascript-object-check-if-property-exists

> Learn about 3 different ways to check if an object contains a specific property in JavaScript.

JavaScript provides several ways to check if a property exists in an object. You can choose one of the following methods to check the presence of a property:

1. `hasOwnProperty()` method
2. `in` operator
3. Comparison with `undefined`

## `hasOwnProperty()` Method

The `hasOwnProperty()` method is part of the object's [prototype](https://attacomsian.com/blog/objects-prototypes-classes-javascript#prototypes) and returns a boolean value (`true` or `false`) indicating whether the object has the specified property as its [own property](https://attacomsian.com/blog/javascript-own-inherited-properties#own-properties).

Let us say you have the following `food` object:

```javascript
const food = {
    pizza: '?',
    burger: '?',
    fries: '?',
    cake: '?'
};
```

The following example uses the `hasOwnProperty()` method to check if the `fries` property exists in the `food` object:

```javascript
const exists = food.hasOwnProperty('fries');

console.log(exists); // true
```

If the property doesn't exist in the object, the `hasOwnProperty()` method returns `false` as shown below:

```javascript
const exists = food.hasOwnProperty('snacks');

console.log(exists); // false
```

Note that the `hasOwnProperty()` method only checks the presence of the specified property in the object's own properties. The own properties are those defined directly on the object.

If the property is a part of the object's prototype, it is not considered as an object's own property. For example, the `hasOwnProperty()` method doesn't detect the `toString` property because it is inherited from the object's prototype:

```javascript
const exists = food.hasOwnProperty('toString');

console.log(exists); // false
```

## `in` Operator

The `in` operator is another way to check the presence of a property in an object in JavaScript. It returns `true` if the property exists in an object. Otherwise, it returns `false`.

Let us use the `in` operator to look for the `cake` property in the `food` object:

```javascript
const food = {
    pizza: '?',
    burger: '?',
    fries: '?',
    cake: '?'
};

'cake' in food;     // true
'drink' in food;    // false
```

Unlike the `hasOwnProperty()` method, the `in` operator looks for the existence of a property within the own properties and also in the inherited properties of an object.

For example, contrary to `hasOwnProperty()`, the `in` operator detects that the inherited `toString` property exists in the `food` object:

```javascript
food.toString;    // function() {...}
'toString' in food;     // true
```

## Comparison with `undefined`

If you try to access a non-existing property from an object, the returned value is `undefined`:

```javascript
food.pizza;     // ?
food.rice;      // undefined
```

The `food.rice` evaluates to `undefined` because the `food` object doesn't contain the `rice` property.

By using this logic, you can compare the property with the `undefined` to check if the property exists in an object:

```javascript
food.pizza !== undefined;     // true
food.rice === undefined;      // false
```

However, if the object contains a property that has `undefined` value, comparing it with `undefined` will incorrectly evaluate to `false`:

```javascript
const user = {
    name: 'John Doe',
    job: undefined
};

user.job !== undefined;     // false
```

As you can see above, even though the `job` property exists (but has `undefined` value), `user.job !== undefined` evaluates to `false` conveying a false impression that the property doesn't exist.

## Summary

We learned about 3 different ways to check if an object contains a specific property.

1. The `hasOwnProperty()` method checks the existence of a property within the own properties of the object.
2. The `in` operator looks for the property in both own properties and inherited properties of an object.
3. Finally, you can compare the property value with the `undefined` to check if it exists. You should use this method only when you're sure that the property value is not `undefined`. 

If you are not concerned about the object inherited properties, the `in` operator is the most suitable method to check the existence of a property. It has a short and concise syntax.
