What is WeakMap in ES6 ? Last Updated : 05 Aug, 2021 Comments Improve Suggest changes Like Article Like Report WeakMap is a new Data Structure or Collection introduced in ES6. WeakMaps allows you to store a collection of Key-Value pairs. It adopts the same properties of Map. The Major difference is that keys of WeakMap cannot be a primitive data type. The keys must be of type object and values can be of any data type. Another major difference is that the key of WeakMap is weakly referenced. It means that whenever an object is used as a key for WeakMap, that object can be garbage collected. It can happen when the reference to that object is lost( i.e. assign that object reference to NULL). And when the object is no longer in use, JavaScript Garbage Collection detects it and frees it from the memory. Therefore keys of WeakMaps are weakly referenced. Syntax: const m = new WeakMap();Parameters: It is an Iterable object whose Elements are in form of Key-Value Pair. Functions of WeakMap: set(key, value): It is used to add an element(Key-Value Pair) to the WeakMap Object.get(key): It returns the Value associated with the specified "key".has(key): This function is used to check if the "key" specified exists in WeakMap or not. Returns true if it is present and false if it is not present.delete(key): Remove an element of specified "key", from the WeakMap Object.Example: JavaScript Code to show the Working of these Functions. JavaScript // Creating a WeakMap Object const m = new WeakMap(); // Adding elements in it // Remember, only object can // be a key in WeakMap obj1 = {} m.set(obj1, "Object 1"); obj2 = {} m.set(obj2, "Object 2"); // Use delete() function m.delete(obj2); // Print the WeakMap console.log(m); // Using get() function to get // specific element in WeakMap console.log(m.get(obj1)); // Using has() function to check // if a particular element is // present in WeakMap or not. console.log(m.has(obj1)); Output: Following is the output of the above code in the browser. Explanation: The First line is printing the contents of the WeakMap.We deleted the obj2 before printing it, therefore it's not displayed in the console.In the Second line, we used the .get(obj1) function, and therefore it returned the Value i.e. Object 1.The third line is we tried to find whether obj1 exist in WeakMap 'm' or not, and it returned true. Comment More infoAdvertise with us Next Article What is WeakMap in ES6 ? vaibhavpatel1904 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Weakmap JavaScript-Questions Similar Reads WeakSet vs WeakMap in JavaScript In JavaScript, there are two types of references strong and weak. The WeakSet and WeakMap are called weak references. Since these are weak references they do not prevent garbage collection if they are the only reference to the object in the memory. These objects are rarely used but are useful when w 2 min read What is the Temporal Dead Zone in ES6 ? Introduction: Before going to start, just think about what these 3 words are saying? Temporal means something temporary, not permanent, and Dead means something which is not working or can say it is in a lifeless state, Zone denotes an area but here we are in programming so this term area will be so 6 min read What is the use of a WeakMap object in JavaScript ? What is a WeakMap? A WeakMap is a collection of key/value pairs where the keys are objects and values are any JavaScript object. It is similar to Map with some differences: It is not enumerable and the keys are private in nature.Allows garbage collection of any values if keys are not referenced from 3 min read Lodash _.isWeakMap() Method Lodash _.isWeakMap() method is used to find whether the given value is a weakmap object or not. It returns True if the given value is a weakmap object. Otherwise, it returns false. Syntax:_.isWeakMap(value);Parameters: value: This parameter holds the value to check.Return Value: This method returns 1 min read What is TypeScript Map file ? Before getting ahead, these are few terms you should be familiar with: TypeScript Source files: These are files written by yourself and are quite easy to Interpret, i.e., it is Human-Readable. Emitted or Transpiled JavaScript Code: This code is equivalent JavaScript code of the TypeScript source fil 3 min read Map in JS A Map is a data structure that stores key-value pairs, where each key is unique. It is similar to an object but has some advantages:Inserts keys in the order they were added.Allows keys of any type, not just strings and symbols.Provides better performance when dealing with large datasets.Creating a 3 min read JavaScript WeakMap A WeakMap in JavaScript is a collection of key-value pairs where the keys are objects, and the values can be any arbitrary value. Unlike regular maps, WeakMap keys are weakly referenced, meaning they don't prevent garbage collection.The keys in a WeakMap can be automatically removed when no longer i 5 min read Next.js ESLint ESLint is a widely-used tool for identifying and fixing problems in JavaScript code. In Next.js projects, integrating ESLint helps ensure code quality and consistency by enforcing coding standards and catching errors early in the development process.In this article, we'll explore how to set up ESLin 3 min read What is the difference between Map and WeakMap in JavaScript ? In this article, we will talk about the difference between Map and WeakMap which are introduced by ES6. Javascript object supports only one key object. For supporting multiple key objects, Then Map comes on this path. Map: A Map is an unordered list of key-value pairs where the key and the value can 4 min read Like