recursal/MDN huggingface dataset:
Set.prototype[@@iterator]()
===========================

The **`[@@iterator]()`** method of `Set` instances implements the iterable protocol and allows `Set` objects to be consumed by most syntaxes expecting iterables, such as the spread syntax and `for...of` loops. It returns a set iterator object that yields the values of the set in insertion order.

The initial value of this property is the same function object as the initial value of the `Set.prototype.values` property.

Try it
------

Syntax
------


```js
set[Symbol.iterator]()
```

### Parameters

None.

### Return value

The same return value as `Set.prototype.values()`: a new iterable iterator object that yields the values of the set.

Examples
--------

### Iteration using for...of loop

Note that you seldom need to call this method directly. The existence of the `@@iterator` method makes `Set` objects iterable, and iterating syntaxes like the `for...of` loop automatically call this method to obtain the iterator to loop over.


```js
const mySet = new Set();
mySet.add("0");
mySet.add(1);
mySet.add({});

for (const v of mySet) {
  console.log(v);
}
```

### Manually hand-rolling the iterator

You may still manually call the `next()` method of the returned iterator object to achieve maximum control over the iteration process.


```js
const mySet = new Set();
mySet.add("0");
mySet.add(1);
mySet.add({});

const setIter = mySet[Symbol.iterator]();

console.log(setIter.next().value); // "0"
console.log(setIter.next().value); // 1
console.log(setIter.next().value); // {}
```

Specifications
--------------

| Specification |
| --- |
| ECMAScript Language Specification # sec-set.prototype-@@iterator |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* `Set`
* `Set.prototype.entries()`
* `Set.prototype.keys()`
* `Set.prototype.values()`
* `Symbol.iterator`
* Iteration protocols
Set.prototype.difference()
==========================

Limited availability
--------------------

This feature is not Baseline because it does not work in some of the most widely-used browsers.

* Learn more
* See full compatibility
* Report feedback
**Experimental:** **This is an experimental technology**  
Check the Browser compatibility table carefully before using this in production.

The **`difference()`** method of `Set` instances takes a set and returns a new set containing elements in this set but not in the given set.

Syntax
------


```js
difference(other)
```

### Parameters

`other`

A `Set` object, or set-like object.

### Return value

A new `Set` object containing elements in this set but not in the `other` set.

Description
-----------

In mathematical notation, *difference* is defined as:



A
∖
B
=
{
x
∊
A
∣
x
∉
B
}

A\setminus B = {x\in A\mid x\notin B}


And using Venn diagram:

![A Venn diagram where two circles overlap. The difference of A and B is the part of A that is not overlapping B.](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/difference/diagram.svg)

`difference()` accepts set-like objects as the `other` parameter. It requires `this` to be an actual `Set` instance, because it directly retrieves the underlying data stored in `this` without invoking any user code. Then, its behavior depends on the sizes of `this` and `other`:

* If there are more elements in `this` than `other.size`, then it iterates over `other` by calling its `keys()` method, and constructs a new set with all elements in `this` that are not seen in `other`.
* Otherwise, it iterates over the elements in `this`, and constructs a new set with all elements `e` in `this` that cause `other.has(e)` to return a falsy value.

The order of elements in the returned set is the same as in `this`.

Examples
--------

### Using difference()

The following example computes the difference between the set of odd numbers (<10) and the set of perfect squares (<10). The result is the set of odd numbers that are not perfect squares.


```js
const odds = new Set([1, 3, 5, 7, 9]);
const squares = new Set([1, 4, 9]);
console.log(odds.difference(squares)); // Set(3) { 3, 5, 7 }
```

Specifications
--------------

| Specification |
| --- |
| Set methods # sec-set.prototype.difference |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* Polyfill of `Set.prototype.difference` in `core-js`
* `Set.prototype.intersection()`
* `Set.prototype.isDisjointFrom()`
* `Set.prototype.isSubsetOf()`
* `Set.prototype.isSupersetOf()`
* `Set.prototype.symmetricDifference()`
* `Set.prototype.union()`
Set.prototype.forEach()
=======================

 The **`forEach()`** method of `Set` instances executes a provided function once
 for each value in this set, in insertion order.

Try it
------

Syntax
------


```js
forEach(callbackFn)
forEach(callbackFn, thisArg)
```

### Parameters

`callback`

A function to execute for each entry in the set. The function is called with the following arguments:

`value`

Value of each iteration.

`key`

Key of each iteration. This is always the same as `value`.

`set`

The set being iterated.


`thisArg` Optional

A value to use as `this` when executing `callbackFn`.

### Return value

None (`undefined`).

Description
-----------

 The `forEach()` method executes the provided
 `callback` once for each value which actually exists in the
 `Set` object. It is not invoked for values which have been deleted. However,
 it is executed for values which are present but have the value `undefined`.

`callback` is invoked with **three arguments**:

* the **element value**
* the **element key**
* the **`Set` object being traversed**

 There are no keys in `Set` objects, however, so the first two arguments are
 both **values** contained in the `Set`. This is to make it
 consistent with other `forEach()` methods for `Map` and `Array`.

 If a `thisArg` parameter is provided to `forEach()`,
 it will be passed to `callback` when invoked, for use as its
 `this` value. Otherwise, the value `undefined` will be passed for
 use as its `this` value. The `this` value ultimately observable by
 `callback` is determined according to
 the usual rules for determining the `this` seen by a function.

 Each value is visited once, except in the case when it was deleted and re-added before
 `forEach()` has finished. `callback` is not invoked for
 values deleted before being visited. New values added before `forEach()` has
 finished will be visited.

`forEach()` executes the `callback` function once for
 each element in the `Set` object; it does not return a value.

Examples
--------

### Logging the contents of a Set object

The following code logs a line for each element in a `Set` object:


```js
function logSetElements(value1, value2, set) {
  console.log(`s[${value1}] = ${value2}`);
}

new Set(["foo", "bar", undefined]).forEach(logSetElements);

// Logs:
// "s[foo] = foo"
// "s[bar] = bar"
// "s[undefined] = undefined"
```

Specifications
--------------

| Specification |
| --- |
| ECMAScript Language Specification # sec-set.prototype.foreach |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* `Array.prototype.forEach()`
* `Map.prototype.forEach()`
Set.prototype.isSupersetOf()
============================

Limited availability
--------------------

This feature is not Baseline because it does not work in some of the most widely-used browsers.

* Learn more
* See full compatibility
* Report feedback
**Experimental:** **This is an experimental technology**  
Check the Browser compatibility table carefully before using this in production.

The **`isSupersetOf()`** method of `Set` instances takes a set and returns a boolean indicating if all elements of the given set are in this set.

Syntax
------


```js
isSupersetOf(other)
```

### Parameters

`other`

A `Set` object, or set-like object.

### Return value

`true` if all elements in the `other` set are also in this set, and `false` otherwise.

Description
-----------

In mathematical notation, *superset* is defined as:



A
⊇
B
⇔
∀
x
∊
B
,

x
∊
A

A\supseteq B \Leftrightarrow \forall x\in B,,x\in A


And using Venn diagram:

![A Venn diagram with two circles. A is a superset of B because B is completely contained in A.](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/isSupersetOf/diagram.svg)

**Note:** The *superset* relationship is not *proper superset*, which means `isSupersetOf()` returns `true` if `this` and `other` contain the same elements.

`isSupersetOf()` accepts set-like objects as the `other` parameter. It requires `this` to be an actual `Set` instance, because it directly retrieves the underlying data stored in `this` without invoking any user code. Then, its behavior depends on the sizes of `this` and `other`:

* If there are fewer elements in `this` than `other.size`, then it directly returns `false`.
* Otherwise, it iterates over `other` by calling its `keys()` method, and if any element in `other` is not present in `this`, it returns `false` (and closes the `keys()` iterator by calling its `return()` method). Otherwise, it returns `true`.
Examples
--------

### Using isSupersetOf()

The set of even numbers (<20) is a superset of multiples of 4 (<20):


```js
const evens = new Set([2, 4, 6, 8, 10, 12, 14, 16, 18]);
const fours = new Set([4, 8, 12, 16]);
console.log(evens.isSupersetOf(fours)); // true
```


The set of all odd numbers (<20) is not a superset of prime numbers (<20), because 2 is prime but not odd:


```js
const primes = new Set([2, 3, 5, 7, 11, 13, 17, 19]);
const odds = new Set([3, 5, 7, 9, 11, 13, 15, 17, 19]);
console.log(odds.isSupersetOf(primes)); // false
```


Equivalent sets are supersets of each other:


```js
const set1 = new Set([1, 2, 3]);
const set2 = new Set([1, 2, 3]);
console.log(set1.isSupersetOf(set2)); // true
console.log(set2.isSupersetOf(set1)); // true
```

Specifications
--------------

| Specification |
| --- |
| Set methods # sec-set.prototype.isSupersetOf |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* Polyfill of `Set.prototype.isSupersetOf` in `core-js`
* `Set.prototype.difference()`
* `Set.prototype.intersection()`
* `Set.prototype.isDisjointFrom()`
* `Set.prototype.isSubsetOf()`
* `Set.prototype.symmetricDifference()`
* `Set.prototype.union()`
Set.prototype.add()
===================

The **`add()`** method of `Set` instances inserts a new element with a specified value in to this set, if there isn't an element with the same value already in this set

Try it
------

Syntax
------


```js
add(value)
```

### Parameters

`value`

The value of the element to add to the `Set` object.

### Return value

The `Set` object with added value.

Examples
--------

### Using the add() method


```js
const mySet = new Set();

mySet.add(1);
mySet.add(5).add("some text"); // chainable

console.log(mySet);
// Set [1, 5, "some text"]
```

Specifications
--------------

| Specification |
| --- |
| ECMAScript Language Specification # sec-set.prototype.add |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* `Set`
* `Set.prototype.delete()`
* `Set.prototype.has()`
Set.prototype.has()
===================

 The **`has()`** method of `Set` instances returns a boolean indicating whether an
 element with the specified value exists in this set or not.

Try it
------

Syntax
------


```js
has(value)
```

### Parameters

`value`

The value to test for presence in the `Set` object.

### Return value

Returns `true` if an element with the specified value exists in the `Set` object; otherwise `false`.

Examples
--------

### Using the has() method


```js
const mySet = new Set();
mySet.add("foo");

console.log(mySet.has("foo")); // true
console.log(mySet.has("bar")); // false

const set1 = new Set();
const obj1 = { key1: 1 };
set1.add(obj1);

console.log(set1.has(obj1)); // true
console.log(set1.has({ key1: 1 })); // false, because they are different object references
console.log(set1.add({ key1: 1 })); // now set1 contains 2 entries
```

Specifications
--------------

| Specification |
| --- |
| ECMAScript Language Specification # sec-set.prototype.has |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* `Set`
* `Set.prototype.add()`
* `Set.prototype.delete()`
Set() constructor
=================

The **`Set()`** constructor creates `Set` objects.

Try it
------

Syntax
------


```js
new Set()
new Set(iterable)
```


**Note:** `Set()` can only be constructed with `new`. Attempting to call it without `new` throws a `TypeError`.

### Parameters

`iterable` Optional

 If an iterable object is passed, all of its elements will be added to the new
 `Set`.


 If you don't specify this parameter, or its value is `null`, the new
 `Set` is empty.


### Return value

A new `Set` object.

Examples
--------

### Using the `Set` object


```js
const mySet = new Set();

mySet.add(1); // Set [ 1 ]
mySet.add(5); // Set [ 1, 5 ]
mySet.add(5); // Set [ 1, 5 ]
mySet.add("some text"); // Set [ 1, 5, 'some text' ]
const o = { a: 1, b: 2 };
mySet.add(o);
```

Specifications
--------------

| Specification |
| --- |
| ECMAScript Language Specification # sec-set-constructor |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* Polyfill of `Set` in `core-js`
* `Set`
Set.prototype.entries()
=======================

The **`entries()`** method of `Set` instances returns a new *set iterator* object that contains **an array of `[value, value]`** for each element in this set, in insertion order. For `Set` objects there is no `key` like in `Map` objects. However, to keep the API similar to the `Map` object, each *entry* has the same value for its *key* and *value* here, so that an array `[value, value]` is returned.

Try it
------

Syntax
------


```js
entries()
```

### Parameters

None.

### Return value

A new iterable iterator object.

Examples
--------

### Using entries()


```js
const mySet = new Set();
mySet.add("foobar");
mySet.add(1);
mySet.add("baz");

const setIter = mySet.entries();

console.log(setIter.next().value); // ["foobar", "foobar"]
console.log(setIter.next().value); // [1, 1]
console.log(setIter.next().value); // ["baz", "baz"]
```

Specifications
--------------

| Specification |
| --- |
| ECMAScript Language Specification # sec-set.prototype.entries |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* `Set.prototype.keys()`
* `Set.prototype.values()`
Set.prototype.size
==================

The **`size`** accessor property of `Set` instances returns the number of (unique) elements in this set.

Try it
------

Description
-----------

The value of `size` is an integer representing how many entries the `Set` object has. A set accessor function for `size` is `undefined`; you cannot change this property.

Examples
--------

### Using size


```js
const mySet = new Set();
mySet.add(1);
mySet.add(5);
mySet.add("some text");

console.log(mySet.size); // 3
```

Specifications
--------------

| Specification |
| --- |
| ECMAScript Language Specification # sec-get-set.prototype.size |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* `Set`
Set.prototype.values()
======================

The **`values()`** method of `Set` instances returns a new *set iterator* object that contains the values for each element in this set in insertion order.

Try it
------

Syntax
------


```js
values()
```

### Parameters

None.

### Return value

A new iterable iterator object.

Examples
--------

### Using values()


```js
const mySet = new Set();
mySet.add("foo");
mySet.add("bar");
mySet.add("baz");

const setIter = mySet.values();

console.log(setIter.next().value); // "foo"
console.log(setIter.next().value); // "bar"
console.log(setIter.next().value); // "baz"
```

Specifications
--------------

| Specification |
| --- |
| ECMAScript Language Specification # sec-set.prototype.values |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* `Set.prototype.entries()`
* `Set.prototype.keys()`
Set.prototype.isSubsetOf()
==========================

Limited availability
--------------------

This feature is not Baseline because it does not work in some of the most widely-used browsers.

* Learn more
* See full compatibility
* Report feedback
**Experimental:** **This is an experimental technology**  
Check the Browser compatibility table carefully before using this in production.

The **`isSubsetOf()`** method of `Set` instances takes a set and returns a boolean indicating if all elements of this set are in the given set.

Syntax
------


```js
isSubsetOf(other)
```

### Parameters

`other`

A `Set` object, or set-like object.

### Return value

`true` if all elements in this set are also in the `other` set, and `false` otherwise.

Description
-----------

In mathematical notation, *subset* is defined as:



A
⊆
B
⇔
∀
x
∊
A
,

x
∊
B

A\subseteq B \Leftrightarrow \forall x\in A,,x\in B


And using Venn diagram:

![A Venn diragram with two circles. A is a subset of B because A is completely contained in B.](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/isSubsetOf/diagram.svg)

**Note:** The *subset* relationship is not *proper subset*, which means `isSubsetOf()` returns `true` if `this` and `other` contain the same elements.

`isSubsetOf()` accepts set-like objects as the `other` parameter. It requires `this` to be an actual `Set` instance, because it directly retrieves the underlying data stored in `this` without invoking any user code. Then, its behavior depends on the sizes of `this` and `other`:

* If there are more elements in `this` than `other.size`, then it directly returns `false`.
* Otherwise, it iterates over the elements in `this`, and returns `false` if any element `e` in `this` causes `other.has(e)` to return a falsy value. Otherwise, it returns `true`.
Examples
--------

### Using isSubsetOf()

The set of multiples of 4 (<20) is a subset of even numbers (<20):


```js
const fours = new Set([4, 8, 12, 16]);
const evens = new Set([2, 4, 6, 8, 10, 12, 14, 16, 18]);
console.log(fours.isSubsetOf(evens)); // true
```


The set of prime numbers (<20) is not a subset of all odd numbers (<20), because 2 is prime but not odd:


```js
const primes = new Set([2, 3, 5, 7, 11, 13, 17, 19]);
const odds = new Set([3, 5, 7, 9, 11, 13, 15, 17, 19]);
console.log(primes.isSubsetOf(odds)); // false
```


Equivalent sets are subsets of each other:


```js
const set1 = new Set([1, 2, 3]);
const set2 = new Set([1, 2, 3]);
console.log(set1.isSubsetOf(set2)); // true
console.log(set2.isSubsetOf(set1)); // true
```

Specifications
--------------

| Specification |
| --- |
| Set methods # sec-set.prototype.isSubsetOf |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* Polyfill of `Set.prototype.isSubsetOf` in `core-js`
* `Set.prototype.difference()`
* `Set.prototype.intersection()`
* `Set.prototype.isDisjointFrom()`
* `Set.prototype.isSupersetOf()`
* `Set.prototype.symmetricDifference()`
* `Set.prototype.union()`
Set.prototype.keys()
====================

The **`keys()`** method of `Set` instances is an alias for the `values()` method.

Syntax
------


```js
keys()
```

### Parameters

None.

### Return value

A new iterable iterator object.

Examples
--------

### Using keys()

The `keys()` method is exactly equivalent to the `values()` method.


```js
const mySet = new Set();
mySet.add("foo");
mySet.add("bar");
mySet.add("baz");

const setIter = mySet.keys();

console.log(setIter.next().value); // "foo"
console.log(setIter.next().value); // "bar"
console.log(setIter.next().value); // "baz"
```

Specifications
--------------

| Specification |
| --- |
| ECMAScript Language Specification # sec-set.prototype.keys |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* `Set.prototype.entries()`
* `Set.prototype.values()`
WeakRef.prototype.deref()
=========================

The **`deref()`** method of `WeakRef` instances returns this `WeakRef`'s target value, or `undefined` if the target value has been garbage-collected.

Syntax
------


```js
deref()
```

### Parameters

None.

### Return value

The target value of the WeakRef, which is either an object or a non-registered symbol. Returns `undefined` if the value has been garbage-collected.

Description
-----------

See the Notes on WeakRefs section of the `WeakRef` page for some important notes.

Examples
--------

### Using deref()

 See the Examples
 section of the `WeakRef` page for the complete example.


```js
const tick = () => {
  // Get the element from the weak reference, if it still exists
  const element = this.ref.deref();
  if (element) {
    element.textContent = ++this.count;
  } else {
    // The element doesn't exist anymore
    console.log("The element is gone.");
    this.stop();
    this.ref = null;
  }
};
```

Specifications
--------------

| Specification |
| --- |
| ECMAScript Language Specification # sec-weak-ref.prototype.deref |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* `WeakRef`
WeakRef() constructor
=====================

The **`WeakRef()`** constructor creates `WeakRef` objects.

Syntax
------


```js
new WeakRef(target)
```


**Note:** `WeakRef()` can only be constructed with `new`. Attempting to call it without `new` throws a `TypeError`.

### Parameters

`target`

The target value the WeakRef should refer to (also called the *referent*). Must be an object or a non-registered symbol.

### Return value

A new `WeakRef` object referring to the given target value.

### Exceptions

`TypeError`

Thrown if `target` is not an object or a non-registered symbol.

Examples
--------

### Creating a new WeakRef object

 See the main `WeakRef`
 page for a complete example.


```js
class Counter {
  constructor(element) {
    // Remember a weak reference to a DOM element
    this.ref = new WeakRef(element);
    this.start();
  }
}
```

Specifications
--------------

| Specification |
| --- |
| ECMAScript Language Specification # sec-weak-ref-constructor |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* `WeakRef`