Skip to content

Latest commit

 

History

History
19 lines (13 loc) · 866 Bytes

what_are_the_differences_between_weakset_and_set.md

File metadata and controls

19 lines (13 loc) · 866 Bytes

What are the differences between WeakSet and Set?

  • A WeakSet holds weak references to its objects, which allows garbage collection when the objects are no longer referenced elsewhere.
  • A Set holds strong references to its elements, preventing garbage collection until the set itself is garbage collected.
  • WeakSet can only store objects, while Set can store any type of value.

Example:

const obj1 = { name: 'John' };
const weakset = new WeakSet([obj1]);
const set = new Set([obj1]);

Tags: basic, JavaScript, collections

URL: https://www.tiktok.com/@jsmentoring/photo/7458240152905846048