Map Object (JavaScript)

A collection of key/value pairs.


mapObj = new Map()  


The keys and values in the collection may be of any type. If you add a value to the collection using an existing key, the new value will replace the old value.


The following table lists the properties of the Map object.

Property Description
constructor Specifies the function that creates a map.
prototype Returns a reference to the prototype for a map.
size Returns the number of elements in a map.


The following table lists the methods of the Map object.

Method Description
clear Removes all elements from a map.
delete Removes a specified element from a map.
forEach Performs the specified action for each element in a map.
get Returns a specified element from a map.
has Returns true if the map contains a specified element.
set Adds a new element to a map.
toString Returns a string representation of a map.
valueOf Returns the primitive value of the specified object.


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.set({x:1}, 3);  

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

document.write("<br />");  

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


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.