1. hasOwnProperty() method

Every JavaScript object has a special method object.hasOwnProperty('myProp') that returns a boolean indicating whether object has a property myProp.

In the following example, hasOwnProperty() determines the presence of properties name và realName:

Bạn đang xem: Determining if an object property exists

const anh hùng = name: "Batman";nhân vật.hasOwnProperty("name"); // => trueanh hùng.hasOwnProperty("realName"); // => false
hero.hasOwnProperty('name') returns true because the property name exists in the object hero.

On the other side, hero doesn’t have realName property. Thus hero.hasOwnProperty('realName') returns false — denoting a missing property.

The method name hasOwnProperty() suggests that it looks in the own properties of the object. The own properties are those defined directly upon the object.

Because of that hasOwnProperty() doesn’t detect the inherited toString property:

const anh hùng = name: "Batman";nhân vật.toString; // => function() ...nhân vật.hasOwnProperty("toString"); // => false

2. in operator

'myProp' in object also determines whether myProp property exists in object.

Let’s use in operator lớn detect the existence of name và realName in nhân vật object:

'name' in nhân vật evaluates to lớn true because nhân vật has a property name.

On the other side, 'realName' in anh hùng evaluates to false because nhân vật doesn’t have sầu a property named 'realName'.

in operator has a short syntax, và I prefer it over hasOwnProperty() method.

The main difference between hasOwnProperty() method and in operator is that the latter checks within own and inherited properties of the object.

That’s why, in contrast to lớn hasOwnProperty(), the in operator detects that anh hùng object contains the inherited property toString:

const anh hùng = name: "Batman";anh hùng.toString; // => function() ..."toString" in hero; // => trueanh hùng.hasOwnProperty("toString"); // => false

3. Comparing with undefined

Accessing a non-existing property from an object results in undefined:

const hero = name: "Batman";nhân vậ; // => "Batman"hero.realName; // => undefined
anh hùng.realName evaluates to lớn undefined because realName property is missing.

Now you can see an idea: you can compare with undefined to determine the existence of the property.

const hero = name: "Batman";anh hù !== undefined; // => truenhân vật.realName !== undefined; // => false

Xem thêm: 100+ Những Câu Tán Gái Hay Nhất Mọi Thời Đại Khiến Cô Nàng Xiêu Lòng !== undefined evaluates lớn true, which shows the existence of property.

On the other side, anh hùng.realName !== undefined is false, which indicates that realName is missing.

Comparing with undefined khổng lồ detect the existence of property is a cheap and dirty approach.

But be aware of false-negatives. If the property exists, but has undefined value (case, however, rarely happening), comparing against undefined evaluates incorrectly to lớn false:

const nhân vật = name: undefined; !== undefined; // => false
Even if the property name exists (but has undefined value), !== undefined evaluates lớn false: which incorrectly indicates a missing property.

4. Summary

There are mainly 3 ways to check if the property exists.

The first way is lớn invoke object.hasOwnProperty(propName). The method returns true if the propName exists inside object, and false otherwise.

hasOwnProperty() searches only within the own properties of the object.

The second approach makes use of propName in object operator. The operator evaluates to true for an existing property, and false otherwise.

in operator looks for properties existence in both own & inherited properties.

Finally, you can simply use object.propName !== undefined & compare against undefined directly.

What’s your preferred way lớn kiểm tra for properties existence?

Like the post? Please share!
Suggest Improvement

Quality posts into lớn your inbox

I regularly publish posts containing:

Important JavaScript concepts explained in simple wordsOverview of new JavaScript featuresHow lớn use TypeScript and typingSoftware kiến thiết và good coding practices

Subscribe to lớn my newsletter khổng lồ get them right inkhổng lồ your inbox.

Join 2658 other subscribers.


About Dmitri Pavlutin
Software developer, tech writer and coach. My daily routine consists of (but not limited to) drinking coffee, coding, writing, coaching, overcoming boredom