JavaScript - WeakSet delete() Method
The JavaScript WeakSet delete() method is used to remove a specified element or object from the current WeakSet. When the add() method appends a new object to the WeakSet, the delete() method can remove that same element. It returns a boolean value true, if the specified element has been successfully removed from the WeakSet, and false otherwise.
The WeakSet in JavaScript is a collection that can only contain objects and non-registered symbols. They cannot store arbitrary values of any type as other collections can like sets.
Syntax
Following is the syntax of JavaScript String delete() method −
weakSetInstance.delete(value)
Parameter
This method accepts a parameter named 'value', which is described below −
value − Is a value to be removed from the WeakSet object.
Return value
This method returns true, if the element has been removed successfully from the WeakSet object, and false otherwise.
Examples
Example 1
If the passed value or object exists in this weakset, it will remove the object from the WeakSet and return true.
In the following example, we are using the JavaScript WeakSet delete() method to remove the appended new object(an empty object{}) by the add() method from this WeakSet object.
<html>
<head>
<title>JavaScript WeakSet delete() Method</title>
</head>
<body>
<script>
const Obj = new WeakSet();
const newObj = {};
document.write("WeakSet.add(newObj): ", Obj.add(newObj));
document.write("<br>------Before remove-----");
document.write("<br>Does WeakSet have an object? ", Obj.has(newObj));
document.write("<br>Is the object removed from this WeakSet? ", Obj.delete(newObj));
document.write("<br>------After remove-----");
document.write("<br>Does WeakSet have an object? ", Obj.has(newObj));
</script>
</body>
</html>
Output
The above program returns the following statements −
WeakSet.add(newObj): [object WeakSet] ------Before remove----- Does WeakSet have an object? true Is the object removed from this WeakSet? true ------After remove----- Does WeakSet have an object? false
Example 2
If the passed value is not found in the WeakSet, it returns false.
In this example, we are using the JavaScript WeakSet delete() method to remove an element 10. However, since 10 is an invalid value, it will not be added to the WeakSet. The delete() method will return false because the element does not exist in the WeakSet.
<html>
<head>
<title>JavaScript WeakSet add() Method</title>
</head>
<body>
<script>
const Obj = new WeakSet();
const num = 10;
document.write("Value: ", num);
try {
document.write("WeakSet.add(newObj): ", Obj.add(num));
} catch (error) {
document.write("<br>", error);
}
document.write("<br>------Before remove-----");
document.write("<br>Does WeakSet have an object? ", Obj.has(num));
document.write("<br>Is the object removed from this WeakSet? ", Obj.delete(num));
document.write("<br>------After remove-----");
document.write("<br>Does WeakSet have an object? ", Obj.has(num));
</script>
</body>
</html>
Output
After executing the above program, it will return 'false'.
Value: 10 TypeError: Invalid value used in weak set ------Before remove----- Does WeakSet have an object? false Is the object removed from this WeakSet? false ------After remove----- Does WeakSet have an object? false
Example 3
Along with the custom objects, we can also add and remove the global object like 'window' using the add() and delete() method to this WeakSet object.
<html>
<head>
<title>JavaScript WeakSet add() Method</title>
</head>
<body>
<script>
const Obj = new WeakSet();
document.write("The method WeakSet.add(window) returns: ", Obj.add(window));
document.write("<br>----Before remove----");
document.write("<br>Does WeakSet has 'window' object? ", Obj.has(window));
document.write("<br>Is the 'window' object removed from this WeakSet? ", Obj.delete(window));
document.write("<br>----After remove----");
document.write("<br>Does WeakSet has 'window' object? ", Obj.has(window));
</script>
</body>
</html>
Output
Once the above program is executed, it will return the following statements as −
The method WeakSet.add(window) returns: [object WeakSet] ----Before remove---- Does WeakSet has 'window' object? true Is the 'window' object removed from this WeakSet? true ----After remove---- Does WeakSet has 'window' object? false