WeakSet() constructor

The WeakSet constructor lets you create WeakSet objects that store weakly held objects in a collection.


new WeakSet()
new WeakSet(iterable)

Note: WeakSet() can only be constructed with new. Attempting to call it without new throws a TypeError.


iterable Optional

If an iterable object is passed, all of its elements will be added to the new WeakSet. null is treated as undefined.


Using the WeakSet object

const ws = new WeakSet();
const foo = {};
const bar = {};


ws.has(foo); // true
ws.has(bar); // true

ws.delete(foo); // removes foo from the set
ws.has(foo); // false, foo has been removed
ws.has(bar); // true, bar is retained

Note that foo !== bar. While they are similar objects, they are not the same object. And so they are both added to the set.


ECMAScript Language Specification
# sec-weakset-constructor

Browser compatibility

BCD tables only load in the browser

See also