You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Creating a SetconstmySet=newSet();// Adding values to the SetmySet.add(1);mySet.add("hello");mySet.add(true);// Checking if a value existsconsole.log(mySet.has(1));// true// Removing a valuemySet.delete("hello");// Iterating through the Setfor(constvalueofmySet){console.log(value);}// Size of the Setconsole.log(mySet.size);// 2// Clearing the SetmySet.clear();
Map in JavaScript
// Creating a MapconstmyMap=newMap();// Adding key-value pairs to the MapmyMap.set("name","Vishal");myMap.set("age",21);// Getting a value using a keyconsole.log(myMap.get("name"));// Vishal// Checking if a key existsconsole.log(myMap.has("age"));// true// Removing a key-value pairmyMap.delete("age");// Iterating through the Mapfor(const[key,value]ofmyMap){console.log(key,value);}// Size of the Mapconsole.log(myMap.size);// 1// Clearing the MapmyMap.clear();
Weak Map in JavaScript
letobj={key: 'value'};// Creating a WeakMapletweakMap=newWeakMap();weakMap.set(obj,'metadata');// Checking if the object still exists in the WeakMapconsole.log(weakMap.has(obj));// true// Removing the strong reference to the objectobj=null;// At this point, the object is no longer strongly referenced// The WeakMap's weak reference will allow the object to be garbage collectedconsole.log(weakMap.has(obj));// false