• JavaScript Video Tutorials

JavaScript - WeakMap.set() Method



In JavaScript, the WeakMap.set() method is used to add or update a key-value pair in a WeakMap object.

This method takes two parameters: the "key" and the "value". If the key already present in the WeakMap, the value associated with the key will be updated to the new value provided. If the key is not present, a new key-value pair will be added to the WeakMap. This method returns the WeakMap object itself, allowing for method chaining.

This method is compatible on almost every browser such as Chrome, Edge, Firefox, Opera, and Safari.

Syntax

Following is the syntax of JavaScript WeakMap.set() method −

set(key, value)

Parameters

This method accepts two parameters. The same is described below −

  • key − It is the key that has to be added or updated in the WeakMap object.

  • value − It is the value that has to be added or updated in the WeakMap object.

Return value

The return value of this method is the WeakMap object itself, allowing method chaining.

Examples

Example 1

In the following example, we are adding a new key-value pair to the WeakMap object using the JavaScript WeakMap.set() method −

<html>
<body>
   <script>
      const weakMap = new WeakMap();
      const key = { id: 1 };
      
      weakMap.set(key, 'varun');
      document.write(weakMap.get(key))
   </script>
</body>
</html> 

After executing the above program, it returns the value ("varun") associated with the provided key ("key") as a result.

Example 2

In the example below, we are adding multiple key-value pairs into an empty WeakMap object −

<html>
<body>
   <script>
      const weakMap = new WeakMap();
      const key1 = { id: 1 };
      const key2 = { id: 2 };
      const key3 = { id: 3 };
      
      weakMap.set(key1, 'varun');
      weakMap.set(key2, 'rohit');
      weakMap.set(key3, 'zayn');
      
      document.write(weakMap.get(key1), "<br>")
      document.write(weakMap.get(key2), "<br>")
      document.write(weakMap.get(key3))
   </script>
</body>
</html>

It will return all the values ("varun", "rohit", "zayn") associated with the provided keys ("key1", "key2", "key3") as a result.

Example 3

In this example, we are updating an existing key's value using the JavaScript set() method −

<html>
<body>
   <script>
      const weakMap = new WeakMap();
      const key = { id: 1 };
      
      weakMap.set(key, 'varun');
      weakMap.set(key, 'rohit');
      weakMap.set(key, 'nikhil');
      
      document.write(weakMap.get(key))
   </script>
</body>
</html> 

It will return the newly inserted "value" (nikhil) for the key "1" as result.

Example 4

Here, the set() method is chained to add multiple key-value pairs to an empty WeakMap object in a single statement −

<html>
<body>
   <script>
      let weakMap = new WeakMap();
      let key1 = {};
      let key2 = {};
      let key3 = {};
      
      weakMap.set(key1, 'apple').set(key2, 'banana').set(key3, 'cherry');
      
      document.write(weakMap.get(key1), "<br>");
      document.write(weakMap.get(key2), "<br>");
      document.write(weakMap.get(key3));
   </script>
</body>
</html>

If we execute the above program, it returns all key's "values" as result.

Advertisements