How to use Hashmap in TypeScript ? Last Updated : 10 Apr, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In TypeScript, One can use the built-in Map data structure to implement a hashmap. A Map can be used to store key-value pairs where keys can be of any type. Syntax:// Create a new empty hashmaplet hashmap: Map<KeyType, ValueType> = new Map();// Add key-value pairs to the hashmaphashmap.set(key1, value1);hashmap.set(key2, value2);// Get the value associated with a keylet value = hashmap.get(key);// Check if a key exists in the hashmaplet exists = hashmap.has(key);// Delete a key-value pair from the hashmaphashmap.delete(key);// Total number of key-value pair hashmap contains.let total = hashMap.size;//Removes the all key-value pairs from the map() objecthashMap.clear();Example: Demonstrating HashMap Operations in JavaScript. We set key-value pairs, retrieve values, check key existence, and utilize delete and clear operations. JavaScript // Create a new hashmap let ageMap: Map<string, number> = new Map(); // Add key-value // pairs to the hashmap ageMap.set("Alice", 30); ageMap.set("Bob", 35); ageMap.set("Charlie", 40); // Get the value associated with a key let bobAge = ageMap.get("Bob"); console .log("Bob's age:", bobAge); // Output: Bob's age: 35 // Check if a key exists in the hashmap let hasCharlie = ageMap.has("Charlie"); console .log("Does Charlie exist?", hasCharlie); // Output: Does Charlie exist? true // Delete a key-value pair from the hashmap ageMap.delete("Charlie"); // Check if Charlie still exists hasCharlie = ageMap.has("Charlie"); console .log("Does Charlie still exist?", hasCharlie); // Output: Does Charlie still exist? false // Create a new empty hashmap let hashmap: Map<string, number> = new Map(); // Add key-value pairs to the hashmap let key1 = "key1"; let value1 = 10; let key2 = "key2"; let value2 = 20; hashmap.set(key1, value1); hashmap.set(key2, value2); // Get the value associated with a key let value = hashmap.get(key1); console .log("Value for key1:", value); // Output: Value for key1: 10 // Check if a key exists in the hashmap let exists = hashmap.has(key2); console .log("Does key2 exist?", exists); // Output: Does key2 exist? true // Delete a key-value pair from the hashmap hashmap.delete(key1); // Total number of key-value // pairs hashmap contains let total = hashmap.size; console .log("Total number of key-value pairs:", total); // Output: Total number // of key-value pairs: 1 // Remove all key-value // pairs from the hashmap hashmap.clear(); console .log("HashMap after clearing:", hashmap); // Output: HashMap after clearing: Map {} Output Bob's age: 35Does Charlie exist? trueDoes Charlie still exist? falseValue for key1: 10Does key2 exist? trueTotal number of key-value pairs: 1HashMap after clearing: Map(0) {} Comment More infoAdvertise with us Next Article How to Get an Object Value By Key in TypeScript A abhaystriver Follow Improve Article Tags : TypeScript Similar Reads How to use Associative Array in TypeScript ? Typescript Associative arrays are objects in which indexes are replaced by user-defined keys. Associative Array can be used like a regular array, but the only difference is that it can be accessed using strings instead of numbers. Syntax:let associativeArray: { [key: string]: string } = { key1: "val 2 min read How to Access Dictionary Value by Key in TypeScript ? Dictionaries are often represented as the objects where keys are associated with the specific values. These TypeScript dictionaries are very similar to JavaScript objects and are used wherever data needs to be stored in key and value form. You can use the below method to access the dictionary values 2 min read Data types in TypeScript In TypeScript, a data type defines the kind of values a variable can hold, ensuring type safety and enhancing code clarity.Primitive Types: Basic types like number, string, boolean, null, undefined, and symbol.Object Types: Complex structures including arrays, classes, interfaces, and functions.Prim 3 min read How to Get an Object Value By Key in TypeScript In TypeScript, we can get an object value by key by accessing the specific properties within the objects of the dynamic type. This can be done using Dot Notation, Bracket Notation, and Optional Chaining. In this article, we will explore all these approaches along with their implementation in terms o 5 min read How to Setup a TypeScript Project? In the world of modern web development, TypeScript has emerged as a powerful superset of JavaScript, offering static typing and improved tooling. Its strong typing system helps developers catch errors early during development, leading to more maintainable and scalable code. Whether you're starting a 2 min read TypeScript Map TypeScript Map is a collection that stores key-value pairs, where keys and values can be of any type. It maintains the insertion order of keys and provides methods to add, retrieve, check, remove, and clear entries, ensuring efficient management of key-value data.Creating a MapA map can be created a 3 min read Like