Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

JavaScript weakSet.has() Method

  • Last Updated : 13 Dec, 2021

Below is the example of weakSet.add() method.

  • Example:




    <script>  
        function gfg() {  
        const A = new WeakSet(); 
       const object = {}; 
       A.add(object); 
       document.write(A.has(object)); 
        }  
        gfg();  
    </script>
  • Output:
    true

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.

Example:

Input: weakset.has(object1); 
Output: true

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.

Supported Browsers:

  • Google Chrome 36 and above
  • Firefox 34 and above
  • Apple Safari 9 and above
  • Opera 23 and above
  • Edge 12 and above


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!