The JavaScript WeakMap delete() method deletes the specified element from a WeakMap object.
Syntax:
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:
<!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:
<!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>