Stores or updates entries of key-value pairs in a Map object.


The .set() method accepts two parameters, the key and the value, and assigns the entry to a Map object:

const map = new Map();
map.set(key, value);

The key and value can either be an object or a variable of any data type.


Key-value pairs are stored in a Map object using the .set() method.

const fruits = new Map();
fruits.set('Apples', 5);
fruits.set('Oranges', 8);

The example above uses .set() to store two entries in the fruits map: one for 5 apples and another for 8 oranges. These entries can be overwritten and have their values changed with .set() as well:

fruits.set('Apples', 2);

The output will be:

Map(2) { 'Apples' => 2, 'Oranges' => 8 }


Instead of executing separate calls to .set() for the same Map object, multiple .set()s can be “chained” together:


All contributors

Looking to contribute?

Learn JavaScript on Codecademy