How to Check If an Object has Key in JavaScript

Programmers often deal with different types of data types while programming. Some of them include numbers, strings, objects, etc. The object data type is one of the most used data types in JavaScript.

What is Javascript Object?

JavaScript Object is a built-in data structure that has two parts in which one is the key and another is the value. There is no concept of indexing objects in JavaScript.

Example

let obj = {

 'name': "John smith",

 'address': '245 frank road salt lake city washington.pin 2334455',

 'Phone number': '+1 4545214551'

};

console.log(obj);

Output

{

  name: 'John smith',

  address: '245 frank road salt lake city washington.pin 2334455',

  'Phone number': '+1 4545214551'

}

The above is an example of an object in javascript. Here we can observe that the object has two main components separated by :(colon). The first component is known as the keys of the object and the second one is known as the value of the object.

How to Check If Object has Key in JavaScript

To check if an object has a key in JavaScript, use the == operator. The double equals (==) or equality operator in JavaScript is used to check whether a key exists in the object.

Example

let obj = {

 'name': "John smith",

 'address': '245 frank road salt lake city washington.pin 2334455',

 'Phone number': '+1 4545214551'

};

console.log(obj);

if (obj['name'] == undefined) {

 console.log("The given key does not exists in the object.")

}

else {

 console.log("The given key exists in the object.")

}

Output

{

  name: 'John smith',

  address: '245 frank road salt lake city washington.pin 2334455',

  'Phone number': '+1 4545214551'

}

The given key exists in the object.

Explanation

In the above code, there is an object named obj. We now check whether there exists a key called name in the object. Since there exists a key called name in the object hence the condition returns false. So the second message got printed in the console.

Using if key in Method

Another popular method used is the usage of the statement if key in <object name>. If the key exists in the object then the condition returns true otherwise it will return false.

Example

let obj = {

 'name': "John smith",

 'address': '245 frank road salt lake city washington.pin 2334455',

 'Phone number': '+1 4545214551'

};

console.log(obj);


if ('name' in obj) {

 console.log("The given key exists in the object.")

}

else {

 console.log("The given key does not exist in the object.")

}

Output

{

  name: 'John smith',

  address: '245 frank road salt lake city washington.pin 2334455',

  'Phone number': '+1 4545214551'

}

The given key exists in the object.

Explanation

In the above code, we have created an object named obj using the let operator. Now we have used the conditional statement :

 if(‘name’ in obj)

This statement evaluates boolean true since the keyword name actually exists in the object named obj. Hence the first message got printed in the console

Conclusion

In the above article, we have learned how to check whether a given key actually exists in the object. Programmers sometimes need to use this to debug the programs. In industrial usage, the objects usually contain too many keys. As a result, programmers prefer to use the above techniques instead of manually checking for the existence of the key in the object.

That’s it for this tutorial.

Related posts

How to Check If Two Objects are Equal in JavaScript

How to Check If Variable Exists in JavaScript

How to Check If Object is Empty in JavaScript

Leave a Comment