check if js object has property

How to Check if JS Object Has Property

JavaScript objects are versatile data structures that allow you to store different types of data as key-value pairs, in a compact manner. These keys are also known as properties. Its format is similar to JSON, and can be easily converted into JSON and vice versa making it simple to transfer data to and from web servers. But if you try to access a JS object property that does not exist, you will get an error and the JS code execution on your page may get terminated. So it is advisable to always check if JS object has property before you access it. In this article, we will learn how to check if JS object has a given property.


How to Check if JS Object Has Property

Let us say you have the following JS object.

data = {
  prop: 'exists'
};

As per the latest browser specifications, it is advisable to use hasOwn() function available for every JS object, to check if property exists. Here is an example.

console.log(Object.hasOwn(data, 'prop'));

The above function will return true if specified property exists, else it will return false.

Generally, web developers also use hasOwnProperty() function for the same purpose. It will also return true if property exists, else it will return false.

data.hasOwnProperty('prop');

Please note, it is advisable to use hasOwn() function instead of hasOwnProperty() since it also works for objects created using Object.create(null) statement and also in case hasOwnProperty() function has been overridden.

Also remember that the above method will return true, even if the property is specifically undefined or null. It basically checks the existence of the property and not its actual value.

data = {
  prop: undefined //or null
};
data.hasOwn('prop');//returns true

Some developers also use the following methods of typeof Object[property] or Object.key they both do not work in all cases especially due to availability of strict mode in web browsers.

typeof data['prop']
OR
if(data.prop == undefined){...}

You can also loop through the properties one by one to check if your desired property is present or not but it is tedious in case your object has many properties.

var x = {
  'key': 1
};

if ('key' in x) {
  console.log('property is present');
}

If you are using third party libraries such as Underscore or Lodash, you can also _.has() function to check if a property exists in a given object, as shown below.

var x = {
  'key': 1
};

_.has(x, 'key'); //returns true

In this article, we have learnt how to check if a JS object contains a specific property. It is always a good practice to check these things before accessing the property, to avoid runtime errors.

Also read:

How to Auto Increment With Prefix As Primary Key in MySQL
How to Set Initial Value & Auto Increment in MySQL
How to Create DMARC Record for Your Domain
MySQL Query to Get Column Names from Table
How to Find Min & Max Values of Column in Pandas

Leave a Reply

Your email address will not be published. Required fields are marked *