| by Arround The Web | No comments

How to Check a Key Exists in a JavaScript Object?

There are multiple ways of checking the existing keys in a JavaScript object. Most of the ways include using methods from other packages. To do that, one generally has to first install that package and then work with the methods written inside it. But in this article, we will be working with the methods that come as default in JavaScript. So, let’s start with the first method.

Method 1: Using the “in” Operator to Find the Existence of a Key

We can use the “in” operator to check for a particular key in an object, just like we can use it to find the existence of a particular character in a string. To demonstrate this, we are going to need an object there create an object with the following lines of code:

var personObject = {
    firstName: "John",
    lastName: "Doe",
    age: 18,
    salary: 2200
}

 
As you can see, this object is about a person and includes details like the first name, last name, age, and salary. Suppose that we want to check whether or not the key “age” is present in our personObject. In that case, search for age in personObject and set the return value in a new variable:

existence = "age" in personObject;

 
After that, we can simply print the value inside the existence variable on the terminal using the console log function like:

console.log(existence);

 
After that, simply execute the program and observe the following result on the terminal:


The true value in the terminal means that the key age does exist in the object personObject.

After that, we also want to check for a key that is not present in the personObject. For this, we are going to use the in operator to find the key “martialStatus” in the personObject like:

existence = "martialStatus" in personObject;

 
And then again, we can simply pass this existence variable to the console log function to print the result on the terminal like:

console.log(existence);

 
Execute the program and observe the following result from the terminal:


As you can see, the result was false meaning that there is no such key as martialStatus inside our personObject.

Method 2: Using the “hasOwnProperty()” Method With the Object

In JavaScript, every object has some of the methods from its prototype. One such method is known as the hasOwnProperty(). This method takes in the key you want to search for in its argument and returns true or false depending upon the presence of the key in that object.

To demonstrate hasOwnProperty(), create an object using the following lines of code:

var car = {
  model: "2015",
  make: "Porsche",
  price: 328000,
  reviews: 4.8,
};

 
As you can already tell, the above lines are to create an object of a car. What we want to find is the presence of the key “make” in the object “car”. For this, apply the hasOwnProperty() method on the car object with the help of a dot operator and pass in the key “make” in its argument like:

existence = car.hasOwnProperty("make");

 
After that, simply pass the existence variable in the console log function to display the result on the terminal like:

console.log(existence);

 
Execute the program for the following outcome:


The output on the terminal is true, which means the car object contains the key make. After that, let’s check for the existence of the key “mileage” in our car object. For this, simply pass the key as mileage in the hasOwnProperty() method’s argument:

existence = car.hasOwnProperty("mileage");

 
To show the result on the terminal, simply pass the variable “existence” in the console log function:

console.log(existence);

 
Execute the program and observe the following output:


The output shows that there is no such key as mileage in the object car.

Conclusion

In JavaScript, we can quickly check the existence of a specific key inside an object with two different methods. The first methods include the use of the in operator, and it returns true if the existence is found otherwise, it returns false. The second method includes the use of a method of the JavaScript Object, which is the hasOwnProperty(). In its argument, you simply pass in the key you want to search for, and it returns true if the key is found in the object. Otherwise, it returns false.

Share Button

Source: linuxhint.com

Leave a Reply