Skip to content
Related Articles
Open in App
Not now

Related Articles

JavaScript weakMap.has() Method

Improve Article
Save Article
Like Article
  • Difficulty Level : Medium
  • Last Updated : 22 Dec, 2022
Improve Article
Save Article
Like Article

The Javascript weakMap.has() is an inbuilt function in JavaScript that is used to return a boolean value which indicates whether an element with a particular key is present in the weakmap object or not. 

Syntax:

weakMap.has(key);

Parameters: It accepts a parameter ‘key’ which is the key of the element which is going to be tested for presence in the object weakmap. 

Return values: It returns true if the element with the specified key is present in the weakmap object otherwise it returns false.

Below are examples of weakMap.has() method.

Example 1: Below is a basic example of weakmap.has() method that returns true.

javascript




<script>
    function gfg() {
    const weakmap = new WeakMap();
    const key = {};
    weakmap.set(key, 'gfg');
      
    console.log(weakmap.has(key));
    }
    gfg();
</script>


Output:

true

Example 2: Here output is false because the key “key1” has not been set at the end of the weakMap object. 

javascript




<script>
    // Creating a WeakMap() object
    const weakmap1 = new WeakMap();
          
    // Creating a key "key1"
    const key1 = {};
          
    // Testing whether the key is present
    // in the weakMap() object or not
    console.log(weakmap1.has(key1));
</script>


Output:

false

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

My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!