JavaScript WeakMap

The WeakMap object in JavaScript is a variant of the Map collection. It maintains elements in the form of key-value pairs, where the keys are weakly referenced. In this context, the keys must be objects, while the corresponding values can be of any type.

Syntax

Example

new WeakMap([iterable])

Parameter

iterable - This term refers to an array as well as other iterable entities where the components exist as key-value pairs.

Points to remember

  • A WeakMap object allows the keys of object type only.
  • In WeakMap, if there is no reference to a key object, they are targeted to garbage collection.
  • In WeakMap, the keys are not enumerable. So, it doesn't provide any method to get the list of keys.
  • A WeakMap object iterates its elements in insertion order.
  • JavaScript WeakMap Methods

Methods Description
delete() It deletes the specified element from the WeakMap object.
get() It returns the value of specified key.
has() It indicates whether the WeakMap object contains the specified value element.
set() It adds or updates the key-value pairs to WeakMap object.

Input Required

This code uses input(). Please provide values below: