WeakMap delete() JavaScript

The JavaScript WeakMap delete() method deletes the specified element from a WeakMap object.

Syntax:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
weakMapObj.delete(key);
weakMapObj.delete(key);
weakMapObj.delete(key);

Parameters:
key: It represents the element to be removed from the WeakMap object.

Returns:
It returns if the specified element is removed successfully, otherwise returns false.

Example 1:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<!DOCTYPE html>
<html>
<body>
<script>
var hello = new WeakMap();
var obj = {};
hello.set(obj, 'HELLO');
document.writeln(hello.has(obj)+"<br>");
hello.delete(obj);
document.writeln(hello.has(obj));
</script>
</body>
</html>
<!DOCTYPE html> <html> <body> <script> var hello = new WeakMap(); var obj = {}; hello.set(obj, 'HELLO'); document.writeln(hello.has(obj)+"<br>"); hello.delete(obj); document.writeln(hello.has(obj)); </script> </body> </html>
<!DOCTYPE html>
<html>
<body>
<script>  
var hello = new WeakMap();  
var obj = {};  
hello.set(obj, 'HELLO');  
document.writeln(hello.has(obj)+"<br>");  
hello.delete(obj);  
document.writeln(hello.has(obj));  
</script>
</body>
</html>

Example 2:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<!DOCTYPE html>
<html>
<body>
<script type="text/javascript">
const testWeakmap = new WeakMap();
const testObject = {};
testWeakmap.set(testObject, 42);
document.write(testWeakmap.delete(testObject));
document.write("</br>");
document.write(testWeakmap.has(testObject));
</script>
</body>
</html>
<!DOCTYPE html> <html> <body> <script type="text/javascript"> const testWeakmap = new WeakMap(); const testObject = {}; testWeakmap.set(testObject, 42); document.write(testWeakmap.delete(testObject)); document.write("</br>"); document.write(testWeakmap.has(testObject)); </script> </body> </html>
<!DOCTYPE html>
<html>
<body>
<script type="text/javascript"> 
const testWeakmap = new WeakMap();
const testObject = {};
 
testWeakmap.set(testObject, 42);
 
document.write(testWeakmap.delete(testObject));
document.write("</br>");
 
document.write(testWeakmap.has(testObject));
 
</script>
</body>
</html>