How to Check If a specific Key Exists in a JavaScript Object?

How to Check If a specific Key Exists in a JavaScript Object?

Motivation

I spend almost hours running into a problem and was not getting why it was happening because to me my reasoning was obvious and was to work. working with firebase and angular, I wrote a method in a service that was to get some data from firebase and depending on the value perform certain logic in the component. Since i am adding some features to a project not build by me using firebase and angularjs, i modify the database in order to fit the new project direction. So i wanted to check if a certain value was existing so my first assumption since i knew that i was to received 'undefined' if it was not there and the logs were telling me that the firebase was getting an 'undefined' and i was stock so after more than an hour revising my code, i decided to change the approach and giving the fact that i was receiving an object, i needed to change the methodology i was using and hence decide to check if that specific key was existing and it works. So that is my motivation of writing this short article about checking if a key exists in a JavaScript object and my advice is if you doubt when receiving an object from an api, do check if a specific property and which you are applying some logic eg if exist before checking if its value match your requirement.

JavaScript Object

An object in JavaScript is a self-contained set of related values and functions. They act as a collection of named properties that map to any JavaScript value such as strings, numbers, booleans, arrays, and functions. If a property’s value is a function, it is known as a method. Another way to think about an object is that it’s like a dictionary where you look up a property name and see the value.

Properties in JavaScript are defined as pairs of keys and values. Keys can be generally thought of as the names of the values stored within JavaScript objects. There are a few different ways in which to check if a specific key exists within a certain object. And depending on how you want the information to be displayed, certain techniques are more advisable for returning results than others. Let’s unlock this a bit… If you want to see all the enumerable keys contained within a JavaScript object, then you can use JavaScript’s

Object.keys()

method. The method looks and works like this:

Assume we have this object:

const obj = {

    name: "hasnode",
  "data type": typeof this,
  logName: function() {
    console.log(this.name);
  },
  logDataType: function() {
    console.log(this["data type"]);
  }           
};

obj.logName(); //hasnode
obj.logDataType(); //object

const objectKeys = Object.keys(obj);
console.log(objectKeys); //["name", "data type", "logName", "logDataType"]

const checkNameKey = obj.hasOwnProperty("name");
console.log(checkNameKey) //true

const checkIdProperty = obj.hasOwnProperty("id");
console.log(checkColorProperty); //false

To get all the enumerable keys contained within obj, simply pass obj to JavaScript’s

 Object.keys()

method. The result of doing so will be this:

const objectKeys = Object.keys(obj);
console.log(objectKeys); //["name", "data type", "logName", "logDataType"]

In order to check if a specific key exists within a JavaScript object, we can use the

Object.prototype.hasOwnProperty()

method. This method is a property of all JavaScript objects; and to use it, you simply pass in a string with the name of the key for which you are checking. It looks like this (assume we’re still using our obj object):

const checkNameKey = obj.hasOwnProperty("name");
console.log(checkNameKey) //true
const checkIdProperty = obj.hasOwnProperty("id");
console.log(checkColorProperty); //false

Notice that this method returns the boolean true if there’s a match, and the boolean false if there is not.

Here we have