Widely availableUse Set when uniqueness is the main concern and fast membership checks matter.

Overview

Set stores unique values only once. It is useful for deduplication, membership checks, and lightweight set operations built from arrays.

Browser support

Feature Desktop Mobile
Chrome
Edge
Firefox
Safari
Chrome Android
Safari iOS
38
12
13
8
38
8

The [Symbol.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 Statements/for...of loops. It returns a set iterator object that yields the values of the set in insertion order.

43
12
36
9
43
9

The Set[Symbol.species] static accessor property is an unused accessor property specifying how to copy Set objects.

51
13
41
10
51
10

The Set() constructor creates Set objects.

38
12
13
8
38
8
Set.Set.iterable allowed

`new Set(iterable)`

38
12
13
9
38
9
Set.Set.null allowed

`new Set(null)`

38
12
37
9
38
9

The add() method of Set instances inserts the specified value into this set, if it is not already present.

38
12
13
8
38
8

The clear() method of Set instances removes all elements from this set.

38
12
19
8
38
8

The delete() method of Set instances removes the specified value from this set, if it is in the set.

38
12
13
8
38
8

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]…

38
12
24
8
38
8

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

38
12
25
8
38
8

The has() method of Set instances returns a boolean indicating whether the specified value exists in this Set or not.

38
12
13
8
38
8
Set.key equality for zeros

Key equality for -0 and 0

38
12
29
9
38
9

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

38
12
24
8
38
8

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

38
12
19
8
38
8

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.

38
12
24
8
38
8
1+Supported (version) Not supported Has note Sub-feature descriptions sourced from MDN Web Docs (CC BY-SA 2.5)
Notes 6 item(s)
Removed
  • This feature was removed in a later browser version (36)
  • This feature was removed in a later browser version (27)
Implementation note
  • Previously available under a different name: @@iterator (27)
  • A placeholder property named `@@iterator` is used.
  • Previously available under a different name: iterator (17)
  • A placeholder property named `iterator` is used.
Notes 1 item(s)
Implementation note
  • From Firefox 13 to Firefox 18, the `size` property was implemented as a `Set.prototype.size()` method, this has been changed to a property in later versions conform to the ECMAScript 2015 specification.

Syntax

JAVASCRIPT
const set = new Set([1, 2, 3, 2, 1]);
console.log(set); // Set {1, 2, 3}

set.add(4);
set.has(3);    // true
set.delete(2);
set.size;      // 3

// Removing duplicates from an array
const unique = [...new Set([1, 1, 2, 3, 3])]; // [1, 2, 3]

Live demo

Uniqueness and basic methods

See how add(), has(), and delete() behave when duplicate values are inserted.

JavaScript
Output
Press the Run button

Simple set operations

Build intersection, union, and difference with familiar array helpers and Set.has().

JavaScript
Output
Press the Run button

Use cases

  • Deduplicating input

    Convert arrays to Set when repeated values should collapse into a single canonical list.

  • Managing tags

    Set is a clean way to track selected filters, active IDs, or applied tags without duplicates.

Cautions

  • Set also compares objects by reference, so two separate objects with the same fields are treated as different values.
  • When order matters across serialization or transport, convert the Set back to an array explicitly.

Accessibility

  • If Set membership changes selected chips, filters, or tags, keep the visible state and announced state in sync.

Powered by web-features