WeakSet() constructor
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2015.
TheWeakSet() constructor createsWeakSet objects.
In this article
Syntax
js
new WeakSet()new WeakSet(iterable)Parameters
iterableOptionalIf aniterable object is passed, all of its elements will be added to the new
WeakSet.nullis treated asundefined.
Examples
>Using the WeakSet object
js
const ws = new WeakSet();const foo = {};const bar = {};ws.add(foo);ws.add(bar);ws.has(foo); // truews.has(bar); // truews.delete(foo); // removes foo from the setws.has(foo); // false, foo has been removedws.has(bar); // true, bar is retainedNote thatfoo !== bar. While they are similar objects,they are notthe same object. And so they are both added to the set.
Specifications
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-weakset-constructor> |