Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

JavaScript Map set() Method

  • Last Updated : 08 Mar, 2021

The map.set() method is used to add key-value pairs to a Map object. It can also be used to update the value of an existing key. Each value must have a unique key so that they get mapped correctly.


GFGmap.set(key, value);

Parameters: This method accepts two parameters as mentioned above and described below:

  • Key: This is the key of the item that has to be added or updated in the Map.
  • Value: This is the value of the item that has to be added or updated in the Map.

The examples below illustrate the set() method:

Example 1:


  let GFGMap = new Map();
  // Adding a key value pair
  GFGMap.set(1, "India");
  // Getting the value by key
  // Updating the existing value
  GFGMap.set(1, "England");
  // Getting the value back

Output :


Example 2: Using set() with chaining


  let myMap = new Map();
  // Adding key value pair with chaining
  myMap.set(1, "Rohan").set(2, "Sohan").set(3, "Trojan");
  // Getting the values back

Output :

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!