weakMap.has()
This method is used to find whether an element is present or not in a weakMap. This method returns a boolean indicating whether an element with the specified key exists in the WeakMap object. If the element is present then true will be executed as output else false will be displayed as the output.
syntax
weakMap.has(obj);
It takes the object as a parameter and scrutinizes whether that object is present or not and results in a boolean output based on the object's presence.
Example
In the following example, initially, a weakMap is created using a 'new' keyword. later on two objects "object1" and "object2" were created and objec1 is assigned but not object2 to the created weakMap. When the has() method is used on both the objects, object1 will result, since it is assigned to weakMap, in true, whereas object2 will result in false. Both the outputs were displayed as shown in the output.
<html> <body> <script> var weakmap = new WeakMap(); var object1 = { "product" : "Tutorix" }; var object2 = { "Greet" : "Hello" }; weakmap.set(object1); document.write(weakmap.has(object1)); document.write("</br>"); document.write(weakmap.has(object2)); </script> </body> </html>
Output
true false