WeakMap has() JavaScript JS

The JavaScript WeakMap has() method to find out if the WeakMap object contains the specified value element.

Syntax:

WeakMapObj.has(key) 

Parameters:
key: It represents the key to be find in WeakMap object.

Returns:
It returns true if the given key is present in the WeakMap, otherwise return false.

Example 1:

<!DOCTYPE html>
<html>
<body>
<script>  
var hello = new WeakMap();  
var obj = {};  
hello.set(obj, 'HELLO');  
document.writeln(hello.has(obj));  
</script>
</body>
</html>

Output:

true

Example 2:

<!DOCTYPE html>
<html>
<body>
<script type="text/javascript"> 
const testWeakmap = new WeakMap();
const object1 = {};
const object2 = {};
 
testWeakmap.set(object1, 'w3spoint');
 
document.write(testWeakmap.has(object1));
document.write("</br>");
 
document.write(testWeakmap.has(object2));
</script>
</body>
</html>

Output:

true
false
Please Share