Related Articles

Related Articles

JavaScript weakSet.delete() Method
  • Last Updated : 18 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 B = {}; 
    A.delete(B); 
      
    document.write(A.has(B) +"<br>"); 
        }  
        gfg();  
    </script>

    chevron_right

    
    

  • Output:
    false

The weakSet.delete() is an inbuilt function in JavaScript which is used to delete an specific element from a weakSet object.

Syntax:

weakSet.delete(A);

Parameters: It accepts a parameter “A” which is a value going to be deleted from the weakset object.

Return Values: It returns true if the element has been removed successfully from the weakset object and false if the element has not been removed successfully or the element is not found in the weakset.



JavaScript code to show the working of this function:
Code #1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
  
   // Constructing weakSet() object
   const A = new WeakSet();
  
   // Creating a new element
   const B = {};
  
   // Adding the element to the weakset object
   A.add(B);
  
   // Testing whether the element has been
   // set into the weakset object or not
   document.write(A.has(B) +"<br>");
  
   // Deleting B form the weakSet() object
   A.delete(B);
  
   // Testing whether the element "B" has been deleted or not
   document.write(A.has(B) +"<br>");
  
</script>

chevron_right


Output:

true
false

Here output is true at first it means that element “B” has been set into the weakSet object successfully and after it false which says that the element “B” has been deleted successfully from 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 :