Check If an Object Contains a Key using Javascript

As a programmer you might also want to have a look at our computer science laptop guide.

tl;dr:

Method 1:

 var valueForKey = "apple" in fruitPrices;

The example above will store true in the variable valueForKey if the key “apple” exists in the object “fruitPrices”

Method 2:

var valueForKey = fruitPrices["apple"];

In method two we will store the value of the key “apple” in the object valueForKey.

You can use either of the two examples above depending on your need.

How to create the object

You are able to create objects that contains key value pairs in JavaScript. They function much like they do in other languages, you are able to look up a value if you provide the key. This is an excellent way of storing values that you want to look up quickly, as you can easily check if an object contains a key or a value using JavaScript.

First create the object that will store your values. We will be using an object where the keys are names of fruits, and the values are the prices of the fruits.

var fruitPrices = {
apple : 2,
banana : 1.55,
orange : 1.35,
avocado : 3
};

If you want to follow along you can copy and paste the code above into the console in your browser. If you then type in the name of the object you should see something like in the image below.

Image of the object and all the key value pairs inside it

Now that we have our object we want to check if the object has a value for a given key. You can do this using the following syntax.

Check if the key exists in the object

The first way to check if a key exists in a JavaScript object is to simply use the key. By passing in the key to the object in the way you see below, it will return the value for you if it exists. You can then check if you have a value or not.

fruitPrices["apple"];

By passing the key “apple” to the object it will return the corresponding value which is 2.

Looking up the value in an object using the key

Other than just looking up the value (which is most common), we might also want to check if a given key already exists in the object. The object can only have unique keys and we might want to check if it already exists before we try to add it to the object. We can do this using the “in” keyword.

Using “in” Keyword to Check if Object Contains Key

var valueForKey = "apple" in fruitPrices;

The above code will store true in the variable valueForKey. You can also type “apple” in fruitPrices; into your console and you can see that it prints true. Make sure that you still have the fruitPrices object in your console too.

Checking if a key already exists can actually be quite useful sometimes.  Lets say you are looping over a list of words, and you want to count how many times each word appears. You could create an object where the key was the word and the value was the count. Each time you see a word you would check to see if it was in the object, if it is then you increment the value. If it is not already in the object then you add the new word and a count of 1 as a new key value pair.

There are many other times when you might want to check if object contains a key in JavaScript, so bookmark this page so that you can have another look when you need to.

Here is a second example with just the code.

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

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