CodesJava

Easy learning with example program codes

WeakMap set() JavaScript

The JavaScript WeakMap set() method is used to add or modify the key-value pairs to WeakMap object. Key must be unique.

Syntax:

WeakMapObj.set(key,value) 

Parameters:
key: It represents the key of the element to be added or modified.
value: It represents the value of the element to added or modified.

Returns:
Updated WeakMap object.

Example 1:

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

Output:

HELLO

Example 2:

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

Output:

codesjava
jai
Please follow and like us:
error








Copyright © 2019 CodesJava DMCA.com Protection Status