Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

JavaScript weakMap.delete() Method

  • Last Updated : 30 Sep, 2021

Below is the example of weakMap.delete() method. 
 

  • Example: 
     

javascript




<script>
    function gfg() {
const weakmap = new WeakMap();
 
const key = {};
weakmap.set(key, 6);
 
document.write(weakmap.delete(key));
    }
    gfg();
</script>
  • Output: 
     
true

The weakMap.delete() is an inbuilt function in JavaScript which is used to delete a particular element from a object WeakMap.
Syntax: 
 

weakMap.delete(key);

Parameters: It accepts a parameter “key” which is the key of the element which is going to be deleted form the object weakMap.
Return values: It returns true if that element has been deleted from the weakMap object and false if that key is not present in the weakMap object.
Example: 
 

Input: weakmap1.delete(key1)
Output: true

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

javascript




<script>
 
   // Creating a WeakMap() object
   const weakmap1 = new WeakMap();
    
   // Creating a key "key1"
   const key1 = {};
    
   // Setting the value 6 with key1 to the
   // the end of weakMap object
   weakmap1.set(key1, 6);
    
   // Deleting key of the element from
   // the weakMap object
   document.write(weakmap1.delete(key1));
 
</script>

Output: 
 

true

Here output is true it means that key of the element has been deleted successfully.
Code #2: 
 

javascript




<script>
 
   // Creating a WeakMap() object
   const weakmap1 = new WeakMap();
    
   // Creating a key "key1"
   const key1 = {};
    
   // Deleting key of the element from
   // the weakMap object
   document.write(weakmap1.delete(key1));
 
</script>

Output: 
 

false

Here output is false because the key “key1” with any value has not been set to the end of the weakMap object.

Supported Browser:

  • Chrome 36 and above
  • Edge 12 and above
  • Firefox 6 and above
  • Internet Explorer 11 and above
  • Opera 23 and above
  • Safari 8 and above
     


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!