Skip to content
Related Articles
Get the best out of our app
Open App

Related Articles

What is the use of a WeakMap object in JavaScript ?

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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 somewhere other than a WeakMap.
  • Only objects are allowed as keys.
  • It depends on garbage collection, introducing non-determinism.

Note: Keys cannot be Primitive data type (E.g. a string cannot be a WeakMap key).


new WeakMap([iterable])

Instance methods: It has 4 instance methods.

  1. WeakMap.prototype.delete( key ): It removes any value with the given key. 
  2. WeakMap.prototype.get( key ): It returns the value with the given key, else undefined.
  3. WeakMap.prototype.has( key ): It returns a boolean value whether a value is a WeakMap object or not.
  4. WeakMap.prototype.set( key, value ): It sets the value of the given key in the WeakMap object.

Uses of WeakMap:

  • It can be used in tracking object calls.
  • It can be used in tracking libraries.
  • It can be used as private property in a class.
  • Keeping data about host objects like DOM nodes in the browser.

Note: Execute the below examples with node.js


  • Install node.js
  • Execute node <filename.js> in CLI

Example 1: This is a basic use case of a WeakMap where we are referencing an empty object as a WeakMap key which gets garbage collected when assigned to “null” and automatically the WeakMap also gets set to “undefined” from “value”.


const wm = new WeakMap();
let obj = {};
wm.set(obj, "value");
obj = null;

Output :


Example 2: In this example, you will understand to track objects with weakmap. You can count the number of times the object is passed to the function foo().


const objTracker = (() => {
    const wm = new WeakMap();
    return (obj) => {
        wm.set(obj, (wm.get(obj) || 0) + 1);
        return wm.get(obj);
function foo(args) {
    const count = objTracker(args);
    console.log(`${JSON.stringify(args)} is called ${count} times`)
const obj1 = { key: 1 }
const obj2 = { key: 2 }


{"key":1} is called 1 times
{"key":1} is called 2 times
{"key":2} is called 1 times

Example 3: In this example, we create private props in class. This is similar to using private property with # as a prefix.


const Foo = (() => {
    const wm = new WeakMap();
    return class Fooclass {
        constructor() {
            wm.set(this, {
                classname: 'Fooclass'
        get name() {
            return wm.get(this).classname;
const obj1 = new Foo()



Example 4: In this example, you will understand to do a special task with respect to the library methods, so that when they are called you can do something special around it. You can do something till the mod() function exists. 


function mod(a, b) {
    return a % b;
const lib = new WeakMap();
lib.set(mod, {
    do: () => {
        console.log('Something done');


Something done

My Personal Notes arrow_drop_up
Last Updated : 05 Jan, 2023
Like Article
Save Article
Similar Reads
Related Tutorials