InterviewHubPRO
Section Overview
Q211

Question 211 of 521

Part 4: Prototypes, Object Methods & Event Loop (174 - 253)

40%
JavaScript Internals (521 Qs)•Part 4: Prototypes, Object Methods & Event Loop (174 - 253)

What is a WeakMap

A WeakMap object is a collection of key/value pairs in which the keys are weakly referenced. In this case, keys must be objects and the values can be arbitrary values. The syntax looks like the following:

javascript
  new WeakMap([iterable]);

Let's see the below example to explain it's behavior,

javascript
  var ws = new WeakMap();
  var user = {};
  ws.set(user);
  ws.has(user); // true
  ws.delete(user); // removes user from the map
  ws.has(user); // false, user has been removed