Home » JavaScript WeakMap set() Method

JavaScript WeakMap set() Method

by Online Tutorials Library

JavaScript WeakMap set() method

The JavaScript WeakMap set() method is used to add or update an element to WeakMap object with the particular key-value pair. Each value must have a unique key (i.e. object).

Syntax

The set() method is represented by the following syntax:

Parameter

key – It represents the key to be added.

value – It represents the value to be added.

Return

The WeakMap object.

JavaScript WeakMap set() method example

Here, we will understand set() method through various examples.

Example 1

Let’s see an example to add elements to WeakMap object.

Test it Now

Output:

jQuery  AngularJS  Bootstrap  

Example 2

Let’s see an example to add elements to WeakMap object in chainable form.

Test it Now

Output:

jQuery  AngularJS  Bootstrap  

Example 3

In this example, we will determine the result when different values are added to the same object.

Test it Now

Output:

jQuery  Bootstrap  Bootstrap  

Next TopicJavaScript WeakMap

You may also like