# Name some methods of weakMap instances in javascript?

WeakMap is a collection in JavaScript. This type of collection is used to store the data in the form of key-value pairs. In WeakMap, the key must be definitely an object and the values can be of any type.

## New function in weakMaps

A new WeakMap is created using the ‘new’ key word dynamically.

### Syntax

A new WeakMap is created using the syntax mentioned below.

var weakMapName = new WeakMap()


### Example 1

This example demonstrates how to create a WeakMap using new operator in JavaScript −

var wkMap = new WeakMap()
if(wkMap){
console.log("WeakMap is created using new operator:",wkMap)
}
else{
console.log("Create the WeakMap")
}


## Set function in weakMap

The created weakmap should have elements in it. To have elements in the created WeakMap a function called set() is used. Set() function takes two parameters, one is the key of object type and the other is the value which is to be kept at the specified key.

### Syntax

To insert elements into the created WeakMap, the following syntax is used.

weakMapName.set(key,value)


## Get function in weakMap

When the weakmap is created and the elements are inserted in to it by using new key word and set method respectively, then to get those elements of the weakmap, another method is to be used i.e., get().

The get() method or function will take the key as the parameter of the weakmap and prints the result.

### Syntax

The syntax of get function is shown below.

weakMapName.get(key)


### Example

This example demonstrates usage of set() and get() of a WeakMap in JavaScript −

var wkMap = new WeakMap()

k1 = {}
k2 = {}
k3 = {}
wkMap.set(k2,'2:Tarun Chandra')

console.log("This is the weakmap created using new operator:",wkMap.get(k1),wkMap.get(k2),wkMap.get(k3))


## Has function in weakMap

In weakmap if an element is to be checked for its existence at a specific key, then a function has() is used. It returns a Boolean result i.e., ‘true’ if the specific element is present in the weakmap and ‘false’ if it not. The has() function takes the key as the parameter.

### Syntax

The syntax of has function is shown below.

weakMap.has(key)


### Example

This example demonstrates the usage of has() function of weakmap in JavaScript −

var wkMap = new WeakMap()

k1 = {}
k2 = {}
k3 = {}
k4 = {}
wkMap.set(k2,'2:Tarun Chandra')

console.log("There is a value at the given key k2",wkMap.has(k2))
console.log("There is a value at the given key k4",wkMap.has(k4))

console.log("Printing the elements in weakmap:",wkMap.get(k1),wkMap.get(k2),wkMap.get(k3))


## The delete() function of weakMap

In a weakmap, elements can also be deleted with ease by just using a function delete(). The delete() function takes one parameter that is the key of the weakmap and deletes the specific element.

### Syntax

The syntax of weakmap delete function is as follows −

weakMapName.delete(key)


### Example

This example demonstrates about the delete() function in weakmap in JavaScript −

var wkMap = new WeakMap()

k1 = {}
k2 = {}
k3 = {}
k4 = {}