# WeakSet

* References to keys/values held weakly
* Do not prevent garbage collection
* Small api ( add, delete, check for values ) compared to set/map
* cann't be iterated

**Note**

* keys must be objects&#x20;
* the values can be arbitrary values.

```javascript
var ws = new WeakSet();
ws.add()
ws.delete()
ws.has()
```

## No clear() method on weakset

As if their is no reference to object wekset runs garbage collection so we have no need to worry about it

```javascript
let dog1 = {name: 'tomy'};
let dog2 = {name: 'fluffy'};

const ws = new WeakSet([dog1, dog2]);
console.log(ws); // WeakSet {Object {name: "fluffy"}, Object {name: "tomy"}}

dog1 =null;

console.log(ws); // WeakSet {Object {name: "fluffy"}}, after some tome
```

## WeakSet Demo

```javascript
var ws = new WeakSet();
var age = {age: 20};
ws.add({name: 'hemant'}).add(age);
console.log(ws.delete(age)); // true
console.log(ws.has(age)); // false
console.log(ws); // WeakSet {Object {name: "hemant"}}
```
