Tutorialspoint.dev

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:

<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>

Output:

true

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

<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>

Output:

false

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



This article is attributed to GeeksforGeeks.org

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter