Favorites

JavaScript WeakSet Object

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
WeakSet
JavaScript Keyed Collections · ConstructorThis object is supported by all modern browsers.
Updated 28th November 2026

A WeakSet is a collection of objects held weakly, so they can still be garbage-collected. It is used to mark objects, for example to remember which ones have already been processed.

Description

A WeakSet stores objects (or non-registered symbols), each at most once, and holds them weakly: being in the set doesn't stop an object from being garbage-collected. That makes it useful for tagging objects, such as tracking which elements have been initialized, without keeping them alive.

Like WeakMap, it can't be iterated and has no size; it only has add(), has() and delete().

Only objects and non-registered symbols can be added; adding a primitive throws a TypeError. A common use is preventing an operation from running twice on the same object, such as attaching listeners to an element, or detecting circular references while walking an object graph. If you need to list the members or know how many there are, use a regular Set.

Creating

new WeakSet()
new WeakSet(iterable)

Example

const seen = new WeakSet();

function process(obj) {
  if (seen.has(obj)) return 'already done';
  seen.add(obj);
  return 'processed';
}

const item = { id: 1 };
console.log(process(item));
console.log(process(item));

Browser Support

The following information will show you the current browser support for the WeakSet object. Hover over a browser icon to see the version that first introduced support for this object.

This object is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee