JS Coding Questions Logo
JS Coding Questions
#204💼 Interview💻 Code

What is a WeakSet

Advertisement

728x90

A WeakSet is used to store a collection of weakly(weak references) held objects. The syntax would be as follows,

javascript
1new WeakSet([iterable]);

Let's see the below example to explain it's behavior,

javascript
1var ws = new WeakSet();
2
3var user = {};
4
5ws.add(user);
6
7ws.has(user); // true
8
9ws.delete(user); // removes user from the set
10
11ws.has(user); // false, user has been removed

Advertisement

Responsive Ad
🎯 Practice NowRelated Challenge

JavaScript Coding Exercise 34

Test your knowledge with this interactive coding challenge.

Start Coding

Advertisement

728x90
204of476