How to Check if a Key Exists in JavaScript Object

In this snippet, we are going to guide you in checking whether a key exists in a JavaScript object or not.

Before checking if a key exists in the object, it is necessary to create the object, which will store your values.

For creating that object, you need to act like this:

var personSalary = {
  engineer: 2500,
  programmer: 4000,
  accountant: 2000,
  lawyer: 3000
};

For following along, you can copy and paste the code given above into the console in your browser.

Then if you type in the name of the object, you should see something like this:

There exist several ways of checking if a key exists in the object. The first one is to use the key. If you pass in the key to the object, it will return the value if it exists. Then you can check whether you have a value or not.

It’s shown in the example below:

personSalary["peogrammer"];

So, when you pass the key “programmer” to the object, it returns the matching value that is 2.

Here is an example:

Aside from looking up the value, you may also wish to check whether a given key exists in the object. The object may have only unique keys, and you might want to check if it already exists before adding one. You can do it by using the in operator, as follows:

var valueForKey = "programmer" in personSalary;

So, the code above stores true inside the variable valueForKey.Also, you can type ”programmer” in personSalary into the console and notice that it prints true again. But it would be best if you make sure that there is a personSalary in your console, as well.

Now, let’s check out another option with just code:

var learnLanguage = {
  javaScript: true,
  python: false,
  cSharp: true,
  java: true
};

var javaScriptExists = "javaScript " in learnLanguage;
console.log("Learn Javascript? " + learnLanguage["javascript"] ? "Yes" : "No");

In case you want to view all the enumerable keys in a JavaScript object, you can use the Object.keys() method, which looks and works as follows:

let obj = {
  objName: "animal",
  "data type": typeof this,
  logName: function () {
    console.log(this.objName);
  },
  logDataType: function () {
    console.log(this["data type"]);
  }
};
obj.logName(); // animal
obj.logDataType(); // object

For getting all the enumerable keys inside obj, just pass obj to the Object.keys() method. The result is the following:

let obj = {
  objName: "animal",
  "data type": typeof this,
  logName: function () {
    console.log(this.objName);
  },
  logDataType: function () {
    console.log(this["data type"]);
  }
};
let x = Object.keys(obj);
console.log(x); //["name", "data type", "logName", "logDataType"]

Describing Objects in JavaScript

In JavaScript, objects are applied for storing keyed collections of various data and more complicated entities.

JavaScript objects are included in all aspects of the language, so you need to learn them as soon as you start to study it.

Objects are created with figure brackets {…} and should have a list of properties. Property is known as a “key: value”, in which key or property name is a string and value can be whatever.


Do you find this helpful?

Related articles