# The Set JavaScript Data Structure

> Learn how JavaScript Set stores unique values, how to add, find, delete and iterate values, combine sets, convert them to arrays, and use WeakSet.

Author: [Flavio Copes](https://flaviocopes.com/about/) | Published: 2018-03-03 | Updated: 2026-07-18 | Topics: [JavaScript](https://flaviocopes.com/tags/js/) | Canonical: https://flaviocopes.com/javascript-data-structures-set/

<!-- TOC -->

- [What is a Set?](#what-is-a-set)
- [Create a Set](#create-a-set)
- [Add and inspect values](#add-and-inspect-values)
- [Delete values](#delete-values)
- [Iterate a Set](#iterate-a-set)
- [Convert between sets and arrays](#convert-between-sets-and-arrays)
- [Combine and compare sets](#combine-and-compare-sets)
- [WeakSet](#weakset)

<!-- /TOC -->

## What is a Set?

A **Set** is a collection of unique values.

It can store primitive values and objects. Values stay in insertion order when you iterate the Set.

Sets use the `SameValueZero` comparison. This means `NaN` equals `NaN`, and `0` equals `-0`.

See the [MDN Set reference](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set) for the complete API.

## Create a Set

Create an empty Set with the `Set` constructor:

```js
const colors = new Set()
```

You can also pass any iterable:

```js
const colors = new Set(['red', 'green', 'blue'])
```

Repeated values are stored once:

```js
const numbers = new Set([1, 1, 2, 2, 3])

numbers.size //3
```

## Add and inspect values

Use `add()` to add one value:

```js
const colors = new Set()

colors.add('red')
colors.add('green')
```

`add()` returns the Set, so you can chain calls:

```js
colors
  .add('blue')
  .add('orange')
```

Adding the same value again does not create a duplicate.

Use `has()` to check for a value:

```js
colors.has('red') //true
colors.has('purple') //false
```

Use the `size` property to count the values:

```js
colors.size //4
```

## Delete values

`delete()` removes one value and reports whether it was present:

```js
colors.delete('orange') //true
colors.delete('orange') //false
```

`clear()` removes every value:

```js
colors.clear()
colors.size //0
```

## Iterate a Set

The simplest approach is a `for...of` loop:

```js
const colors = new Set(['red', 'green', 'blue'])

for (const color of colors) {
  console.log(color)
}
```

`values()` and `keys()` return equivalent iterators:

```js
for (const color of colors.values()) {
  console.log(color)
}
```

The `entries()` iterator returns `[value, value]` pairs. This shape keeps Set compatible with APIs designed for Map:

```js
for (const [key, value] of colors.entries()) {
  console.log(key, value)
}
```

You can also use `forEach()`:

```js
colors.forEach(color => {
  console.log(color)
})
```

## Convert between sets and arrays

Spread a Set into an array:

```js
const colors = new Set(['red', 'green', 'blue'])
const list = [...colors]
```

This also gives you a short way to remove duplicate array values:

```js
const numbers = [1, 1, 2, 3, 3]
const uniqueNumbers = [...new Set(numbers)]

uniqueNumbers //[1, 2, 3]
```

## Combine and compare sets

Modern JavaScript provides methods for common set operations.

Create a union containing values from both sets:

```js
const frontEnd = new Set(['HTML', 'CSS'])
const programming = new Set(['JavaScript', 'CSS'])

const all = [...frontEnd.union(programming)]

all //['HTML', 'CSS', 'JavaScript']
```

Other composition methods are:

- `intersection()`, for values in both sets
- `difference()`, for values in the first Set but not the second
- `symmetricDifference()`, for values in either Set but not both

You can compare sets with:

- `isSubsetOf()`
- `isSupersetOf()`
- `isDisjointFrom()`

These methods return new sets or booleans. They do not change the original Set.

The composition methods are part of ES2025. Check compatibility when supporting older JavaScript engines.

## WeakSet

A **WeakSet** stores garbage-collectable values. Those values can be objects or non-registered symbols.

If nothing else references an object in a WeakSet, JavaScript can reclaim it. A regular Set keeps a strong reference to every value it contains.

Create a WeakSet and add an object:

```js
const visited = new WeakSet()
const page = { url: '/about' }

visited.add(page)
visited.has(page) //true
```

A WeakSet is not iterable and has no `size` or `clear()`. It provides `add()`, `has()`, and `delete()`.

Use it when you want to associate membership with objects without keeping those objects alive.
