set Method (WeakMap) (JavaScript)


Adds a new element to a WeakMap object.

weakmapObj.set(key, value)


Required. A WeakMap object.


Required. An object representing the key of the element to add. This must be an object reference.


Required. The value of the element to add.

Returns the WeakMap object that contains the new key/value pair.

If you add a value to the collection using an existing key, the new value will replace the old value.


The following example shows how to add members to a WeakMap object.

var dog = {
    breed: "yorkie"

var cat = {
    breed: "burmese"

var wm = new WeakMap();
wm.set(dog, "fido");
wm.set(cat, "pepper");


Supported in the Internet Explorer 11 standards document mode. Also supported in Store apps (Windows 8.1 and Windows Phone 8.1). See Version Information.

Not supported in the following document modes: Quirks, Internet Explorer 6 standards, Internet Explorer 7 standards, Internet Explorer 8 standards, Internet Explorer 9 standards, Internet Explorer 10 standards. Not supported in Windows 8.