Related Articles

Related Articles

JavaScript weakSet.has() Method
  • Last Updated : 17 Sep, 2020

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

  • Example:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

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

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.

Supported Browsers:

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Apple Safari
  • Opera

full-stack-img




My Personal Notes arrow_drop_up
Recommended Articles
Page :