Wednesday, September 25, 2024
Google search engine
HomeLanguagesJavascriptJavaScript weakSet delete() Method

JavaScript weakSet delete() Method

JavaScript weakSet.delete() method is used to delete a specific element from a weakSet object. The WeakSet object lets you store weakly held objects in a collection.

Syntax:

weakSet.delete(value);

Parameters: This method accepts a single parameter value.

  • value: This value will 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.

Below are examples of weakSet.delete() method:

Example 1:

javascript




function gfg() {  
    const A = new WeakSet(); 
    const B = {}; 
    A.delete(B); 
  
    console.log(A.has(B)); 
}  
gfg();  


Output:

false

Example 2: Here output is true at first which means that element “B” has been set into the weakSet object successfully and after it is false it says that the element “B” has been deleted successfully from the weakSet object. 

javascript




// 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
console.log(A.has(B));
  
// Deleting B form the weakSet() object
A.delete(B);
  
// Testing whether the element "B" has been deleted or not
console.log(A.has(B));


Output:

true
false

Supported Browsers:

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

We have a complete list of Javascript weakSet methods, to check those please go through this JavaScript WeakSet Complete Reference article.

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments