GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.geeksforgeeks.org — JavaScript WeakMap() Constructor - GeeksforGeeks JavaScript WeakMap() ConstructorLast Updated : 23 Jul, 2025 The WeakMap() Constructor produces WeakMap objects that are a key/value pair array in which the key is referenced weakly. The keys should be objects and the values could be arbitrary. The difference between Map and WeakMap is that keys must be objects and are only weakly referenced. This means that if there are no other strong references to the key, the garbage collector can remove the element in WeakMap. Syntax: new WeakMap( iterable ) Parameters: It accepts an optional parameter that can be any iterable object. An iterable is an array-like object with key-value pairs in the elements. The created WeakMap will include each key-value pair. The null is considered undefined. The below examples illustrate the WeakmMap Constructor: Example 1: The get() method is used to retrieve a value associated with the key. If no value is associated with the key, it returns undefined.
Output: 17 undefined Example 2: The set() method assigns a value to the key. It returns the WeakMap object, which allows you to chain.set() calls.
Output: 100 200 Example 3: The has() method is used to determine whether an element with a given key exits in a WeakMap. It returns true if it exits otherwise, it returns false.
Output: true false Example 4: The delete() method is used to delete an element with a specific key. It returns true if the element existed and was removed otherwise, it returns false.
Output: false true We have a complete list of Javascript weakMap methods, to check those please go through this JavaScript WeakMap Complete Reference article. We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript. Comment Explore |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |