JS: WeakSet

By Xah Lee. Date: . Last updated: .

what is WeakSet

WeakSet is similar to the the Set Object , with 3 exceptions:

  1. Item can only be Object Type, or unregistered Symbol type (meaning, symbols user created, not builtin symbols.).
  2. If a item in the set is not used elsewehere in your program, or no longer used, that item is deleted automatically.
  3. WeakSet cannot be iterated over, nor get its item count, nor clear all.

All methods for WeakSet.

// demo, of all methods for WeakSet

const ws = new WeakSet();

const xkey = { "hi": 3 };

// add
ws.add(xkey);

// has
console.assert(ws.has(xkey) === true);

// delete
ws.delete(xkey);

console.assert(ws.has(xkey) === false);

JavaScript, map and set