WeakSet.prototype.add()

The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.

Syntax

ws.add(value);

Parameters

value

Required. The object to add to the WeakSet collection.

Return value

The WeakSet object.

Using the add method

var ws = new WeakSet();
ws.add(window); // add the window object to the WeakSet
ws.has(window); // true
// Weakset only takes objects as arguments
ws.add(1);
// results in "TypeError: Invalid value used in weak set" in Chrome
// and "TypeError: 1 is not a non-null object" in Firefox