Open In App

JavaScript weakSet has() Method

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.

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: 




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:




// 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: 




// 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:

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


Article Tags :