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 found in the WeakMap object.

Returns:
It returns true if the given key is present in the WeakMap, 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));  
</script>
</body>
</html>

Example 2:

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