JavaScript Set Data Structure

A JavaScript Set is a collection of unique values. Each value can only occur once in a Set. The values can be of any type, primitive values or objects.

You can create a JavaScript Set by:

Passing an array to new Set()

Example:

// Create a Set
const letters = new Set(["a","b","c"]);
Creating an empty set and use add() to add values

Example:

// Create a Set
const letters = new Set();

// Add Values to the Set
letters.add("a");
letters.add("b");
letters.add("c");

If you add equal elements, only the first will be saved.

JavaScript Set Methods and Properties

The size Property

// Create a new Set
const mySet = new Set(["a","b","c"]);

// The number of elements are
mySet.size;

Listing with for ... of

You can list all Set elements (values) with a for...of... loop:

// Create a Set
const letters = new Set(["a","b","c"]);

// List all Elements
let text = "";
for (const x of letters) {
  text += x;
}

The has() Method

The has() method returns true if a specified value exists in a set.

// Create a Set
const letters = new Set(["a","b","c"]);

// Does the Set contain "d"?
answer = letters.has("d");

The forEach() Method

The forEach() method invokes a function for each Set element:

// Create a Set
const letters = new Set(["a","b","c"]);

// List all entries
let text = "";
letters.forEach (function(value) {
  text += value;
})

The keys() and values() Methods

They both return an Iterator object with the values in a Set:

// Create a Set
const letters = new Set(["a","b","c"]);

// List all entries
let text = "";
letters.forEach (function(value) {
  text += value;
})

The entries() Method

The entries() method returns an Iterator with [value,value] pairs from a Set.

Note. The entries() method is supposed to return a [key,value] pair from an object. On the other hand, a Set has no keys, so the entries() method returns [value,value]. This makes Sets compatible with Maps.

// Create a Set
const letters = new Set(["a","b","c"]);

// Get all Entries
const myIterator = letters.entries();

// List all Entries
let text = "";
for (const entry of myIterator) {
  text += entry;
}

Set Logic

In JavaScript 2025, 7 new logical methods were added to the Set object:

union()

The union() method returns the union of two sets. The union() method returns a new set containing the elements which are in this set, or in the argument set, or in both:

const A = new Set(['a','b','c']);
const B = new Set(['b','c','d']);

const C = A.union(B);

difference()

The difference() method returns the difference between two sets. The difference() method returns a new set containing elements which are in this set but not in the argument set:

const A = new Set(['a','b','c']);
const B = new Set(['b','c','d']);

const C = A.difference(B);

intersection()*

The intersection() method returns the intersection of two sets. The intersection() method returns a new set containing the elements which are in this set and in the argument set:

const A = new Set(['a','b','c']);
const B = new Set(['b','c','d']);

const C = A.intersection(B);

isDisjointFrom()*

The isDisjointFrom() method returns true if this set has no elements in common with the argument set:

const A = new Set(['a','b','c']);
const B = new Set(['b','c','d']);

let answer = A.isDisjointFrom(B);

isSubsetOf()

The isSubsetOf() method returns true if all elements in this set is also elements in the argument set:

const A = new Set(['a','b','c']);
const B = new Set(['b','c','d']);

let answer = A.isSubsetOf(B);

isSupersetOf()*

The isSupersetOf() method returns true if all elements in the argument set are also in this set:

const A = new Set(['a','b','c']);
const B = new Set(['b','c','d']);

let answer = A.isSupersetOf(B);

symmetricDifference()*

The symmetricDifference() method returns the symmetric difference between to sets. The symmetricDifference() method returns a new set containing elements which are in this set or in the argument set, but not in both:

const A = new Set(['a','b','c']);
const B = new Set(['b','c','d']);

const C = A.symetricDifference(B);

Complete Set Reference

Method/Property Description
new Set() Creates a new set
add(element) Adds a new element to a set
clear Removes all elements from a set
delete(element) Removes an element from a set
difference(anotherSet) Returns the difference between two sets
entries() Returns an Iterator with the [value,value] pairs from a set
forEach(func) Invokes a callback for each element in a set
has Returns true if a value exists
intersection() Returns the intersection of two sets
isDisjointFrom() Returns true if no elements in a set are elements in another set
isSubsetOf() Returns true if a set is a subset of another set
isSupersetOf() Returns true if a set is a superset of another set
keys() Same as values()
symmetricDifference Returns the symmetric difference between two set
union() Returns the union of two sets
values() Returns an Iterator with the values in a set
size() Returns the number of elements in a Set