WeakMap has() JavaScript JS

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

Syntax:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
WeakMapObj.has(key)
WeakMapObj.has(key)
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:

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));
</script>
</body>
</html>
<!DOCTYPE html> <html> <body> <script> var hello = new WeakMap(); var obj = {}; hello.set(obj, 'HELLO'); 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));  
</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 object1 = {};
const object2 = {};
testWeakmap.set(object1, 'w3schools');
document.write(testWeakmap.has(object1));
document.write("</br>");
document.write(testWeakmap.has(object2));
</script>
</body>
</html>
<!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>
<!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>