Open In App

JavaScript weakSet has() Method

Improve
Improve
Like Article
Like
Save
Share
Report

JavaScript weakSet.has() method is used to return a boolean value indicating whether an object is present in a weakSet or not. The WeakSet object lets you store weakly held objects in a collection.

Syntax:

weakSet.has(value);

Parameters: This method accepts a single parameter value.

  • value: This value is going to be checked whether it is present in the weakSet object or not.

Return Values: It returns a boolean value true if the element is present otherwise it returns false.

Below are examples of weakSet.has() method:

Example 1: 

javascript




function gfg() {
    const A = new WeakSet();
    const object = {};
    A.add(object);
    console.log(A.has(object));
}
gfg();


Output: Here output is true because the object “object” is present in the WeakSet() object.

true

Example 2:

javascript




// Constructing a weakSet() object
const A = new WeakSet();
  
// Constructing new objects
const object1 = {};
  
// Adding the new object to the weakSet
A.add(object1);
  
// Checking whether the new object is present
// in the weakSet or not
console.log(A.has(object1));


Output: Here output is true because the object “object1” is present in the WeakSet() object.

true

Example 3: 

javascript




// Constructing a weakSet() object
const A = new WeakSet();
  
// Constructing new objects
const object1 = {};
  
// Checking whether the new object is present
// in the weakSet or not
console.log(A.has(object1));


Output: Here the output is false because the object “object1” is not present in the WeakSet() object. 

false

Supported Browsers:

  • Google Chrome 36 and above
  • Firefox 34 and above
  • Apple Safari 9 and above
  • Opera 23 and above
  • Edge 12 and above

We have a complete list of Javascript weakSet methods, to check those please go through this JavaScript WeakSet Complete Reference article.



Last Updated : 22 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Similar Reads