JavaScript | weakSet.has() with Examples

The weakSet.has() is an inbuilt function in JavaScript which is used to return a boolean value indicating whether an object is present in a weakSet or not. The WeakSet object lets you store weakly held objects in a collection.
Syntax:

weakSet.has(A);

Parameters: It accepts the parameter “A” which is a value going to be checked whether it is present in the weakSet object or not.
Return Values: It returns boolean value true if the element is present otherwise it returns false.

JavaScript code to show the working of this function:

Code #1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
  
   // Constructing a weakSet() object
   const A = new WeakSet();
  
   // Constructing new objects
   const object1 = {};
  
   // Adding the new object to the weakSet
   A.add(object1);
  
   // Checking whether the new object is present
   // in the weakSet or not
   document.write(A.has(object1) +"<br>");
  
</script>

chevron_right


Output:



true

Here output is true because the object “object1” is present in the WeakSet() object.
Code #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
  
   // Constructing a weakSet() object
   const A = new WeakSet();
  
   // Constructing new objects
   const object1 = {};
  
   // Checking whether the new object is present
   // in the weakSet or not
   document.write(A.has(object1) +"<br>");
  
</script>

chevron_right


Output:

false

Here output is false because the object “object1” is not present in the WeakSet() object.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.