set Method (Map) (JavaScript)

Adds a new element to a map.

Syntax

mapObj.set(key, value)  

Parameters

mapObj
Required. A Map object.

key
Required. The key for the new element.

value
Required. The value of the element to add.

Property Value/Return Value

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

Remarks

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

Example

The following example shows how to add members to a Map and then retrieve them.

var m = new Map();  
m.set(1, "black");  
m.set(2, "red");  
m.set("colors", 2);  

m.forEach(function (item) {  
    document.write(item.toString() + "<br />");  
});  

// Output:  
// black  
// red  
// 2  

Requirements

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.