JavaScript | weakSet.add() with Examples
The weakSet.add() is an inbuilt function in JavaScript which is used to add an object at the end of the a object WeakSet. The WeakSet object lets you store weakly held objects in a collection.
Syntax:
weakSet.add(A);
Parameters: It accepts the parameter “A” which is a value going to be added to the weakset object.
Return Values: It returns the weakset object.
Code #1:
<script> // Constructing a weakset object const weakset = new WeakSet(); // Constructing a new object object1 const object1 = {}; const object2 = {}; const object3 = {}; const object4 = {}; // Adding the object1 at the end of the weakset object. weakset.add(object1); weakset.add(object2); weakset.add(object3); weakset.add(object4); // Printing either object has been added or not document.write(weakset.has(object1) +"<br>"); document.write(weakset.has(object2) +"<br>"); document.write(weakset.has(object3) +"<br>"); document.write(weakset.has(object4)); </script> |
Output:
true true true true
Code #2:
<script> // Constructing a weakset object const weakset = new WeakSet(); // Constructing a new object object1 const object1 = {}; const object2 = {}; const object3 = {}; const object4 = {}; // Printing either object has been added or not document.write(weakset.has(object1) +"<br>"); document.write(weakset.has(object2) +"<br>"); document.write(weakset.has(object3) +"<br>"); document.write(weakset.has(object4)); </script> |
Output:
false false false false
Here the output is false because the new created objects has not been set to the end of the weakSet() object.
Recommended Posts:
- JavaScript | typedArray.every() with Examples
- JavaScript | typedArray.map() with Examples
- JavaScript | _.reduceRight() with Examples
- JavaScript | weakSet.has() with Examples
- JavaScript | unescape() with examples
- JavaScript | getPrototypeOf() with Examples
- JavaScript | parseInt() with Examples
- JavaScript | uneval() with Examples
- JavaScript | console.log() with Examples
- JavaScript | weakMap.set() with Examples
- JavaScript | typedArray.of() with Examples
- JavaScript | weakMap.has() with Examples
- JavaScript | typedArray.name with Examples
- Javascript | _.reduce() with Examples
- JavaScript | typedArray.from() with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



