How to check if a key exists in a JavaScript object?
Gulshan Saini
June 01, 2020
1 min

Let’s consider following user object

To find if, age property/key exist in user object we have following two options

JavaScript in operator

First, is JavaScript in operator which we can use as follows

This gives output of true


Using obj.hasOwnProperty(prop) method

The obj.hasOwnProperty(prop) method returns a true if the key exist in object otherwise false.

And, here is how we can use hasOwnProperty() to check if age exist in user object


Tip: hasOwnProperty() checks the objects own properties whereas in operator matches all object keys, including those in the object's prototype chain.


  • The in operator should be used to check if property exist in object itself or in its prototype chain.
  • obj.hasOwnProperty(prop) checks if the key exist in the object only.


Previous Article
How to get inner html with JavaScript?

Subscribe to our newsletter!

We'll send you the best of our blog just once a month. We promise.

Related Posts

How to Get Current Date in Human Readable Form in JavaScript?
October 03, 2020
1 min
© 2020, All Rights Reserved.

Quick Links

Advertise with usContact Us

Social Media