Open In App

What is Weakset() in JavaScript ?

Last Updated : 26 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

`WeakSet` is a built-in object in JavaScript introduced with ECMAScript 2015 (ES6) that provides a collection for storing weakly held object references. It is similar to a `Set`, but there are a few key differences.

In a `WeakSet`, only objects can be added as values, and each object can occur only once in the set. The key distinction lies in the “weak” aspect – unlike a `Set`, a `WeakSet` does not prevent its elements from being garbage-collected if there are no other references to those objects in the program. This makes `WeakSet` useful for scenarios where you want to associate data with objects without preventing those objects from being cleaned up when they are no longer needed. `WeakSet` does not have iteration methods, size property, or clear method, and its primary operations are `add`, `has`, and `delete`. It is commonly used in scenarios where temporary associations between objects need to be established without creating memory retention issues, promoting more efficient memory management in certain use cases.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads