What is the use of the Map.set() method in JavaScript?

The Map.set() method is a built-in function in JavaScript that allows you to add or update key-value pairs in a Map object.

A Map object is a collection of key-value pairs where each key is unique and can be of any data type (including objects and functions).

The set() method takes two parameters: a key and a value. The key can be any data type, while the value can be any JavaScript value. If the Map already contains the specified key, the set() method updates the value associated with that key. Otherwise, the method adds a new key-value pair to the Map.

Here are some examples that demonstrate the use of the Map.set() method:

Example 1: Adding key-value pairs to a Map:

In this example, we create a new Map object and add some key-value pairs to it using the set() method.

const myMap = new Map();

myMap.set("key1", "value1");
myMap.set("key2", 42);
myMap.set("key3", { name: "John", age: 30 });
myMap.set("key4", function () {
  console.log("Hello, world!");
});

console.log(myMap); // Map(4) {"key1" => "value1", "key2" => 42, "key3" => Object, "key4" => function}

In this example, we create a new Map object called myMap using the Map() constructor. We then use the set() method to add four key-value pairs to the Map. The first key-value pair has a string key ('key1') and a string value ('value1'). The second key-value pair has a string key ('key2') and a number value (42). The third key-value pair has a string key ('key3') and an object value ({name: 'John', age: 30}). The fourth key-value pair has a string key ('key4') and a function value (function() {console.log('Hello, world!')}).

After adding the key-value pairs to the Map, we log the Map object to the console using console.log(). The output shows that the Map contains four key-value pairs with the specified keys and values.

Example 2: Updating the value of an existing key in a Map:

In this example, we create a new Map object and add a key-value pair to it. We then update the value of the key using the set() method.

const myMap = new Map();

myMap.set("key1", "value1");
console.log(myMap); // Map(1) {"key1" => "value1"}

myMap.set("key1", "new value");
console.log(myMap); // Map(1) {"key1" => "new value"}

In this example, we create a new Map object called myMap using the Map() constructor. We then use the set() method to add a key-value pair with a string key ('key1') and a string value ('value1') to the Map. We log the Map object to the console using console.log() and verify that it contains the expected key-value pair.

We then use the set() method again to update the value of the 'key1' key to 'new value'. We log the Map object to the console again and verify that the value of the 'key1' key has been updated.

Example 3: Adding non-string keys to a Map:

In this example, we create a new Map object and add some key-value pairs to it using non-string keys.

const myMap = new Map();

myMap.set(42, "answer");
myMap.set({ name: "John", age: 30 }, "person");
myMap.set(function () {
  console.log("Hello, world!");
}, "function");

console.log(myMap); // Map(3) {42 => "answer", Object => "person", function => "function"}

In this example, we create a new Map object called myMap using the Map() constructor. We then use the set() method to add three key-value pairs to the Map. The first key-value pair has a number key (42) and a string value ('answer'). The second key-value pair has an object key ({name: 'John', age: 30}) and a string value ('person'). The third key-value pair has a function key (function() {console.log('Hello, world!')}) and a string value ('function').

After adding the key-value pairs to the Map, we log the Map object to the console using console.log(). The output shows that the Map contains three key-value pairs with non-string keys and the specified values.

Example 4: Chaining Map.set() calls:

In this example, we chain multiple set() method calls to add key-value pairs to a Map object.

const myMap = new Map()
  .set("key1", "value1")
  .set("key2", 42)
  .set("key3", { name: "John", age: 30 })
  .set("key4", function () {
    console.log("Hello, world!");
  });

console.log(myMap); // Map(4) {"key1" => "value1", "key2" => 42, "key3" => Object, "key4" => function}

In this example, we create a new Map object called myMap using the Map() constructor. We then chain multiple set() method calls to add four key-value pairs to the Map. Each set() method call adds a single key-value pair to the Map.

After adding the key-value pairs to the Map, we log the Map object to the console using console.log(). The output shows that the Map contains four key-value pairs with the specified keys and values.

Thank you for reading, and let’s have conversation with each other

Thank you for reading my article. Let’s have conversation on Twitter and LinkedIn by connecting.

Read more: