Open In App

How to Check if a Key Exists in a Dictionary in TypeScript ?

Last Updated : 23 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In TypeScript dictionaries are used whenever the data is needed to be stored in key and value form. We often retrieve the data from the dictionaries using an associated key. Therefore it becomes crucial to check whether the key exists in a dictionary or not.

We can use the below methods to check if a key exists or not in a dictionary.

Using an ‘in’ operator

The ‘in’ operator checks whether a specific key or a property exists in an object or not. It is a widely used approach as it provides a simplistic way to check the existence of a key in TypeScript dictionaries.

Syntax:

if(key in dictionary){
// key exists
} else{
// key does not exist
}

Example: The below code uses the ‘in’ operator to check if a key exists in a dictionary in TypeScript.

Javascript




interface Dictionary{
    [key:string]:string;
}
let dictionary:Dictionary = {
    "name": "geeksforgeeks",
    "desc": "A computer science portal",
    "est": "2009" };
let keyToCheck:string = "other";
 
if (keyToCheck in dictionary) {
    console.log(
        `Key ${keyToCheck} exists in the dictionary.`);
} else {
    console.log(
        `Key ${keyToCheck} doesn't exist in the dictionary.`);
}


Output:

"Key other doesn't exist in the dictionary."

Using the hasOwnProperty method

The ‘hasOwnProperty’ method is a TypeScript built-in method that we use to check if an object has a property with a specific key. It returns a boolean value and is used when we want to explicitly check for the existence of a property.

Syntax

if(dictionary.hasOwnProperty(key)){
// key exists
} else{
// key does not exist
}

Example: The below code uses the ‘hasOwnProperty’ method to check if a key exists in dictionary in TypeScript.

Javascript




interface Dictionary{
    [key:string]:string;
}
let dictionary:Dictionary = {
    "name": "geeksforgeeks",
    "desc": "A computer science portal",
    "est": "2009" };
let keyToCheck:string = "name";
 
if (dictionary.hasOwnProperty(keyToCheck)) {
    console.log(
        `Key ${keyToCheck} exists in the dictionary.`);
} else {
    console.log(
        `Key ${keyToCheck} doesn't exist in the dictionary.`);
}


Output:

"Key name exists in the dictionary."

Using the undefined keyword

We know that the JavaScript objects returns an undefined if a key does not exist. Being a superset of JavaScript, TypeScript also returns an undefined if a key does not exist. Therefore, we can perform a simple check for undefined to determine the existence of a key in a dictionary in TypeScript.

Syntax

if(dictionary[key] !== undefined){
// Key exists
} else{
// Key does not exist
}

Example: The below code uses the ‘undefined’ keyword to check if a key exists in dictionary in TypeScript.

Javascript




interface Dictionary{
    [key:string]:string;
}
let dictionary:Dictionary = {
    "name": "geeksforgeeks",
    "desc": "A computer science portal",
    "est": "2009" };
let keyToCheck:string = "subjects";
 
if (dictionary[keyToCheck] !== undefined) {
    console.log(
        `Key ${keyToCheck} exists in the dictionary.`);
} else {
    console.log(
        `Key ${keyToCheck} doesn't exist in the dictionary.`);
}


Output:

"Key subjects doesn't exist in the dictionary."

Using the Map object

Similar to JavaScript, TypeScript also provides a dedicated data structure called as ‘Map‘ to store key-value pairs. It has ‘has’ method that is used to check if a specific key exists in the map. It can be used as a alternative to object-based approaches.

Syntax

if(map.has(key)){
// key exists
} else{
// key does not exist
}

Example: The below code uses the Map object to check if a key exists in dictionary in TypeScript.

Javascript




let map = new Map<string, string>();
map.set('name', 'geeksforgeeks');
map.set('desc', 'A computer science portal');
map.set('est', '2009');
let keyToCheck:string = "desc";
 
if (map.has(keyToCheck)) {
    console.log(
        `Key ${keyToCheck} exists in the dictionary.`);
} else {
    console.log(
        `Key ${keyToCheck} doesn't exist in the dictionary.`);
}


Output:

"Key desc exists in the dictionary."


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads