• JavaScript Video Tutorials

JavaScript - WeakSet add() Method



The JavaScript WeakSet add() method is used to append a new object to the end of this WeakSet and returns a WeakSet object. It accepts one parameter named 'value', and it can be either an object or a non-registered symbol.

Note − It throws a 'TypeError' exception if the value passed to this method is invalid.

The WeakSet in JavaScript is a collection that can only contain objects and non-registered symbols. They cannot store arbitrary values of any type as other collections can like sets.

Syntax

Following is the syntax of JavaScript String add() method −

add(value)

Parameter

This method accept a parameter name 'value', which is described below −

  • value − It can be either an object or a non-registered symbol.

Return value

This method returns a WeakSet object.

Examples

Example 1

If the value passed to this method is an object, it appends to the end of this WeakSet.

In the following example, we are using the JavaScript WeakSet add() method to append a new object (an empty object {}) to the end of this WeakSet.

<html>
<head>
   <title>JavaScript WeakSet add() Method</title>
</head>
<body>
   <script>
      const Obj = new WeakSet();
      const newObj = {};
      document.write("WeakSet.add(newObj): ", Obj.add(newObj));
      document.write("<br>WeakSet.has(newObj): ", Obj.has(newObj));
   </script>    
</body>
</html>

Output

The above program returns "[object WeakSet]".

WeakSet.add(newObj): [object WeakSet]
WeakSet.has(newObj): true

Example 2

If the value passed to this method is neither an object nor a non-registered symbol, it will throw a 'TypeError' exception.

The following is another example of the JavaScript WeakSet add() method. In this example, we are trying to append a value "Hello" that is neither an object nor a non-registered symbol to the end of this WeakSet.

<html>
<head>
   <title>JavaScript WeakSet add() Method</title>
</head>
<body>
   <script>
      const Obj = new WeakSet();
      const str = "Hello";
      document.write("Value: ", str);
      try {
         document.write("<br>WeakSet.add(newObj): ", Obj.add(str));
      } catch (error) {
         document.write("<br>", error);
      }
   </script>    
</body>
</html>

Output

After executing the above program, it throws a 'TypeError' exception.

Value: Hello
TypeError: Invalid value used in weak set

Example 3

In addition to custom objects, you can also append the global objects like 'window' using the JavaScript WeakSet add() method to the end of this WeakSet.

<html>
<head>
   <title>JavaScript WeakSet add() Method</title>
</head>
<body>
   <script>
      const Obj = new WeakSet();
      document.write("----Before appending----");
      document.write("<br>Does WeakSet has 'window' object? ", Obj.has(window));
      document.write("<br>The 'window' object appended to this WeakSet: ", Obj.add(window));
      document.write("<br>----After appending----");
      document.write("<br>Does WeakSet has 'window' object? ", Obj.has(window));
   </script>    
</body>
</html>

Output

Once the above program is executed, it will return the following statements as −

----Before appending----
Does WeakSet has 'window' object? false
The 'window' object appended to this WeakSet: [object WeakSet]
----After appending----
Does WeakSet has 'window' object? true
Advertisements