WeakMap, as the name suggests, is an object in which keys are weakly referenced to the key/value pairs. That means that values can be anything. The keys are weakly referenced as keys are objects.
Here are the differences between Map and WeakMap objects:
- Keys of a Map object can be of primitive types or object references but keys in a WeakMap object can only be object references
- One of the important features of a WeakMap object is that if there is no other reference to an object that is referenced by a key then the key is garbage-collected
- Lastly, a WeakMap object is not enumerable, that is, you cannot find its size and it doesn't implement the iterable protocol
In every other particular, apart from these three differences Map ...