JavaScript WeakMap is used to store a collection of key-value pairs where keys are weakly referenced. The keys are objects, and values are arbitrary values. Keep in mind that in weakMap, keys are not enumerable.
Syntax
new WeakMap()
new WeakMap(iterable)
Parameters
iterable: It has two elements, the first is key, and the second is value.
Example 1: How to Use JavaScript WeakMap
let weakMap = new WeakMap();
let key1 = {};
let key2 = {};
weakMap.set(key1, "Ronaldo");
weakMap.set(key2, "Messi");
console.log(weakMap.get(key1));
console.log(weakMap.get(key2));
Output
Ronaldo
Messi
JavaScript WeakMap Methods
Methods | Description |
delete() | It deletes an element with a specific key. |
get() | It is used to return the value associated with the key. |
has() | It is used to determine whether a specified key exists within the WeakMap. |
set() | It assigns a value to the key. |
In WeakMap, Iteration and keys(), entries(), values() methods are not supported.

Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.