JavaScript Set

Beginner
8 min read
Updated: Nov 2026
3 examples
Unique values

Definition and Usage

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.

This page explains the idea. The Set Methods index links the full guides for every method, from add() to union().

New Set methods need a recent browser.

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.

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

Core Ideas

Ideas every Set tutorial in this series builds on:

IdeaExampleRemember
Setnew Set([1, 2, 2, 3])A collection where every value appears only once
sizeset.sizeThe 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
Iterationfor (const v of set)Values come out in the order they were added
Set methodsa.union(b)Join, compare and test Sets (ES2025)

Set vs Array

Both hold a list of values. The difference is how they treat duplicates and lookups:

FeatureArraySet
DuplicatesAllowedNot allowed: a repeated value is ignored
Read by positionarray[0]No index: loop or spread to read values
Check a valueincludes() (looks at items one by one)has() (built for fast lookups)
Countarray.lengthset.size
OrderKeptKept (order of adding)
Remove one valuesplice() 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).

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.

Real-Life Uses

Sets are small but very handy. Common uses:

UseHow a Set helps
Remove duplicatesTurn an array into a Set and back to get a list of unique values: [...new Set(array)].
Fast membership checksKeep a list of allowed names, seen ids or visited pages, and check them with has().
Tags and permissionsStore tags or permissions where each one should appear only once.
Compare two listsFind what two lists share, or what changed, with intersection() and symmetricDifference().

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.

JavaScript
const numbers = [1, 2, 2, 3, 3, 3];
const unique = new Set(numbers);

console.log("Array length: " + numbers.length);
console.log("Set size: " + unique.size);
console.log("Unique: " + Array.from(unique).join(", "));
console.log("typeof: " + typeof unique);
console.log("Has 2: " + unique.has(2));
add() tutorial

How It Works

  1. Start with repeats. The array has 6 items, but only the numbers 1, 2 and 3 are different.
  2. Make a Set. new Set(numbers) adds each number and ignores the ones it already has. The size is 3, and Array.from() turns it back into an array.
  3. Check a value. A Set is an object, and has(2) answers true because 2 was kept once.

Example 2 — Add, Delete and Loop

Add values one after another, remove one, loop over the rest, and finally empty the Set.

JavaScript
const tags = new Set();

tags.add("js").add("css").add("js");
console.log("Size after adding: " + tags.size);

console.log("Delete css: " + tags.delete("css"));
tags.add("html");

for (const tag of tags) {
  console.log("Tag: " + tag);
}

tags.clear();
console.log("Size after clear: " + tags.size);
delete() tutorial

How It Works

  1. Add values. add() returns the Set, so calls can be chained. "js" was added twice, but it is only stored once, so the size is 2.
  2. Delete and add. delete("css") returns true because it was inside. "html" is added after "js".
  3. Loop and clear. for...of prints the values in the order they were added. clear() then removes everything, so the size is 0.

Example 3 — What Counts as the Same Value?

See how a Set treats NaN, the two zeros, a string and a number, and objects.

JavaScript
const values = new Set([NaN, NaN, 0, -0, "5", 5]);

console.log("Size: " + values.size);
console.log("Has NaN: " + values.has(NaN));
console.log("indexOf finds NaN: " + ([NaN].indexOf(NaN) !== -1));

const a = {};
const b = {};
const objects = new Set([a, b, a]);
console.log("Objects: " + objects.size);
has() tutorial

How It Works

  1. Count the different values. NaN counts once, 0 and -0 count as one, and "5" and 5 are different, so there are 4.
  2. NaN is found. A Set treats NaN as equal to itself, so has(NaN) is true, while indexOf() on an array cannot find it.
  3. Objects by identity. a and b look the same but are different objects. Adding a twice stores it once, so the size is 2.

Set Methods

A Set has 15 methods plus the special [Symbol.iterator]() method. Each one has its own tutorial:

MethodWhat it does
add()Put a value into a Set. A repeated value is ignored.
clear()Remove every value from a Set.
delete()Remove one value and get true or false.
difference()Values that are in one Set but not in another (ES2025).
entries()An iterator of [value, value] pairs.
forEach()Run a function for every value.
has()Check whether a value is in the Set.
intersection()Values that two Sets have in common (ES2025).
isDisjointFrom()Check that two Sets share nothing (ES2025).
isSubsetOf()Check that all values are inside another Set (ES2025).
isSupersetOf()Check that a Set contains all values of another (ES2025).
keys()An iterator of the values. The same as values().
symmetricDifference()Values that are in only one of two Sets (ES2025).
union()Join two Sets into one new Set (ES2025).
values()An iterator of every value.
[Symbol.iterator]()The special method behind for...of and spread.

Full list: Set Methods index.

Frequently Asked Questions

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.

Ready for the method guides?

Open the Set Methods index to learn every method with syntax, runnable examples and FAQs.

Set Methods index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

16 people found this page helpful