Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

JavaScript · Class

WeakSet

WeakSet is a collection of unique objects and non-registered symbols held weakly, so unreachable members may be garbage-collected. It supports membership checks and mutation but cannot be enumerated.

How it works

WeakSet stores unique objects and non-registered symbols through weak references. It supports adding, checking, and deleting individual values, while intentionally providing no enumeration mechanism, so values can become garbage-collectable when nothing else strongly references them.

Syntax

new WeakSet()
new WeakSet(iterable)

Parameters

NameTypeDescription
iterableIterableOptional iterable whose elements are added to the new WeakSet. Each element must be an object or a non-registered symbol; null is treated as undefined.

Returns

A newly created WeakSet object.

Key points

  • Its element type is restricted to garbage-collectable objects and non-registered symbols.
  • Adding the same object repeatedly does not create duplicate membership.
  • Weak membership allows otherwise unreachable objects to be reclaimed.
  • Membership uses SameValueZero semantics; for supported object values, identity determines equality.
  • Two separate objects with identical properties remain different WeakSet elements.
  • add(value) inserts a supported value and returns the WeakSet instance.

Common mistakes

  • You cannot iterate over a WeakSet or inspect its size; use has() when you already have a candidate value.
  • Calling WeakSet() as a function throws a TypeError; create instances with new WeakSet().

Example: Track object identity

const seen = new WeakSet();

const first = { id: 1 };
const second = { id: 1 };

seen.add(first);
console.log(seen.has(first));  // true
console.log(seen.has(second)); // false

seen.delete(first);
console.log(seen.has(first));  // false

The set tracks the first object by identity; a separate object with identical contents is not the same value.

When to use it

Non-retaining object tagging

Track whether objects have already been processed without extending their lifetime.

Garbage-collection-friendly state

Associate temporary membership with objects in caches or lifecycle-sensitive workflows.

Visited-object tracking

Prevent repeated handling of object references during graph or tree traversal.

Interview questions

QWhat makes WeakSet different from Set?

WeakSet is useful when membership should not keep an object alive. Its references are weak, so an object may be garbage-collected when no other strong references remain.

Follow-up: How can code detect when an unreferenced member has been collected? It cannot observe that directly, because WeakSet deliberately has no enumeration or size mechanism.

QWhich values can a WeakSet contain, and how are they compared?

WeakSet accepts objects and non-registered symbols, stores each value at most once, and compares objects by identity rather than by their contents.

Follow-up: What happens with two separately created objects containing the same properties? They are distinct references, so both can be members.

QWhich operations does WeakSet provide?

The main instance operations are add(), has(), and delete(). add() returns the WeakSet, has() returns a boolean, and delete() removes a matching value.

Follow-up: What does has() return for a primitive or registered symbol? It returns false rather than treating that value as a valid member.

  • Set
  • WeakMap
  • WeakRef
  • WeakSet.prototype.add()
  • WeakSet.prototype.delete()
  • WeakSet.prototype.has()

Source: WeakSet - JavaScript - MDN Web Docs. Checked 2026-10-15.