A Set is a built-in JavaScript collection that stores unique values. If you add the same value twice, the Set keeps only one copy. It remembers the order in which the values were added, and it can hold any type of value: numbers, strings, objects and more.
A Set is a good choice when you only care about "is this value in the collection?" and you do not want duplicates. It is easier than an array for removing repeated values, and checking a value with has() is fast even for large collections.
The methods union(), intersection(), difference(), symmetricDifference(), isSubsetOf(), isSupersetOf() and isDisjointFrom() were added in ES2025. They work in current browsers and in Node.js 22 or newer. The other methods work everywhere.
Syntax
JavaScript Syntax
Create a Set, add and check values, and loop over it:
JavaScript
const fruits = new Set(["apple", "banana", "apple"]);
console.log(fruits.size); // 2 (the duplicate is ignored)
fruits.add("cherry");
console.log(fruits.has("banana")); // true
fruits.delete("apple");
for (const fruit of fruits) {
console.log(fruit);
}
// banana
// cherry
Reference
Core Ideas
Ideas every Set tutorial in this series builds on:
Idea
Example
Remember
Set
new Set([1, 2, 2, 3])
A collection where every value appears only once
size
set.size
The number of values (a property, not a method)
add()
set.add(value)
Puts a value in; a repeated value is ignored
has()
set.has(value)
Checks if a value is in the Set: true or false
delete()
set.delete(value)
Removes one value and says if it was there
Iteration
for (const v of set)
Values come out in the order they were added
Set methods
a.union(b)
Join, compare and test Sets (ES2025)
Compare
Set vs Array
Both hold a list of values. The difference is how they treat duplicates and lookups:
Feature
Array
Set
Duplicates
Allowed
Not allowed: a repeated value is ignored
Read by position
array[0]
No index: loop or spread to read values
Check a value
includes() (looks at items one by one)
has() (built for fast lookups)
Count
array.length
set.size
Order
Kept
Kept (order of adding)
Remove one value
splice() or filter()
delete(value)
Use an array when order, positions or duplicates matter. Use a Set when you want unique values or fast "is it in there?" checks. You can move between them with new Set(array) and Array.from(set).
Behavior
How a Set Works
Values are unique
When you add a value that is already inside, nothing happens. This is why new Set([1, 1, 2]) has only two values.
Same value, not same look
A Set compares numbers and strings by value, so "5" and 5 are different. NaN counts as equal to itself. Two objects are the same only if they are the very same object.
Order of adding
A Set remembers the order in which values were added. A value that is added again keeps its first place, and a deleted value that is added later goes to the end.
No index
A Set has no set[0]. To read the values, loop with for...of, use forEach(), or turn it into an array with [...set].
Methods that make new Sets
The ES2025 methods such as union() and intersection() return a new Set and leave the original Sets unchanged.
Practice
Real-Life Uses
Sets are small but very handy. Common uses:
Use
How a Set helps
Remove duplicates
Turn an array into a Set and back to get a list of unique values: [...new Set(array)].
Fast membership checks
Keep a list of allowed names, seen ids or visited pages, and check them with has().
Tags and permissions
Store tags or permissions where each one should appear only once.
Compare two lists
Find what two lists share, or what changed, with intersection() and symmetricDifference().
Code
JavaScript Examples
Three short programs. Each one prints plain text that is the same every time you run it, and they all run in Node.js.
Example 1 — Create a Set and Remove Duplicates
Turn an array that has repeated numbers into a Set, and see that only the unique numbers remain.
A Set is a built-in collection that stores unique values. If you add a value that is already inside, it is ignored. A Set keeps the order in which values were added and can hold values of any type.
An array allows duplicates and reads values by position, such as array[0]. A Set keeps every value only once, has no index, and checks values quickly with has(). Use an array for ordered lists and a Set for unique values.
Turn the array into a Set and back: const unique = [...new Set(array)]. The Set drops the repeated values and keeps the first of each, in order.
Use the size property: set.size. Note that it is a property, not a method, so there are no parentheses. An array uses length instead.
union(), intersection(), difference(), symmetricDifference(), isSubsetOf(), isSupersetOf() and isDisjointFrom() came with ES2025. They work in current browsers and Node.js 22 or newer. The other methods have been available since ES2015.