Saturday, December 28, 2024
Google search engine
HomeLanguagesJavascriptJavaScript weakMap get() Method

JavaScript weakMap get() Method

The Javascript weakMap.get() is an inbuilt function in JavaScript that is used to return a particular element from an object WeakMap. 

Syntax:

weakMap.get(key);

Parameters: It accepts a parameter “key” which is the key of the element which is going to be returned from the object weakmap. 

Return values: It returns the element which is associated with the particular key in the WeakMap object and it returns undefined if the key can not be found.

Below is an example of weakMap.get() method.

Example 1: This example shows the basic use of the weakmap().get method.

javascript




function gfg() {
    const weakmap1 = new WeakMap();
    const key1 = {};
 
    weakmap1.set(key1, 12);
 
    console.log(weakmap1.get(key1));
}
gfg();


Output

12


Example 2: Here we get the output as 42 because we have set the value to 42.

javascript




// Creating a WeakMap() object
const weakmap1 = new WeakMap();
 
// Creating a key "key1"
const key1 = {};
 
// setting value 42 with key "key1" in the
// object weakMap
weakmap1.set(key1, 42);
 
// Getting the specified elements i.e, 42
console.log(weakmap1.get(key1));


Output

42


Example 3: In this example, we will see the basic use of weakmap.get() method,.Here the output is undefined because the key “key1” has not been set at the end of the weakMap object.

javascript




// Creating a WeakMap() object
const weakmap1 = new WeakMap();
 
// Creating a key "key1"
const key1 = {};
 
// Getting the specified elements
console.log(weakmap1.get(key1));


Output

undefined


We have a complete list of Javascript WeakMap methods, to check those please go through Javascript WeakMap Complete reference article.

Supported Browsers:

  • Google Chrome 36 and above
  • Edge 12 and above
  • Firefox 6 and above
  • Internet Explorer 11 and above
  • Opera 23 and above
  • Safari 8 and above
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments