Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

WeakSet

A set of objects that does not prevent their garbage collection.

Available since: ES2015

Syntax

new WeakSet(iterable)

Parameters

ParameterTypeDescription
iterableIterableOptional. Objects to start with.

Return value

WeakSetA new set of objects.

Examples

A processed mark:

const processed = new WeakSet();
const order = { id: 7 };
 
processed.add(order);
 
console.log(processed.has(order));
console.log(processed.has({ id: 7 }));

Output:

true
false

Notes

It stores only objects, has no size and cannot be iterated. It suits marking objects without adding properties to them.

See also

  • Set — A collection of unique values.
  • WeakMap — A Map whose keys must be objects and do not prevent garbage collection.

In the tutorial

  • Map and Set — The Map and Set collections in JavaScript: a dictionary with keys of any type and a set of unique values.

Thanks for reading!

Donate to the site — we work hard for you.