SNIPPETJul 23 20264 min read

JavaScript Set Explained Like You're 10

Learn what JavaScript Set is with simple examples. Understand why it is useful, how it is different from arrays, and why it makes many tasks much easier.

If you have worked with Python before, you probably already know about Set.

For JavaScript developers, it was one of those features I always wished we had years ago. Thankfully, JavaScript now has a built-in Set, and it makes many everyday tasks much easier.

Let's see why.

Imagine a Toy Box

You have a toy box.

Every time you get a new toy, you put it inside.

But there is one rule.

You can never have two of the same toy.

If you already have a toy car and someone gives you another toy car, your toy box simply ignores it.

That is exactly how a JavaScript Set works.

A Set stores unique values only.

const toys = new Set();

toys.add("Car");
toys.add("Robot");
toys.add("Car");

console.log(toys);

Output:

Set(2) { 'Car', 'Robot' }

Even though we added "Car" twice, it only appears once.


Arrays Allow Duplicates

An array does not care if values repeat.

const toys = ["Car", "Robot", "Car"];

console.log(toys);

Output:

["Car", "Robot", "Car"]

Sometimes that is exactly what you want.

But many times, duplicates are just extra work.


A Real World Example

Imagine people are entering a conference.

You want to know which countries people came from.

Many people may come from the same country.

const countries = [
  "Japan",
  "USA",
  "Japan",
  "Canada",
  "USA",
  "Bangladesh",
];

With an array, you have duplicates.

With a Set:

const uniqueCountries = new Set(countries);

console.log(uniqueCountries);

Output:

Set(4) {
  'Japan',
  'USA',
  'Canada',
  'Bangladesh'
}

Done.

No loops.

No extra code.


Checking If Something Exists

Let's say your website has premium members.

const premiumUsers = new Set([
  "Alice",
  "Bob",
  "Charlie",
]);

Checking if Bob is a premium member is easy.

premiumUsers.has("Bob");

Returns:

true

Checking a missing user:

premiumUsers.has("David");

Returns:

false

This is also much faster than searching through a large array.


Adding New Values

const fruits = new Set();

fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");

Easy.


Removing Values

fruits.delete("Banana");

Now Banana is gone.


Removing Everything

fruits.clear();

Your set is now empty.


Getting the Number of Items

Arrays use length.

Sets use size.

const colors = new Set([
  "Red",
  "Green",
  "Blue",
]);

console.log(colors.size);

Output:

3

Turning an Array into a Set

This is probably the most common use.

const numbers = [1, 2, 2, 3, 4, 4, 5];

const uniqueNumbers = new Set(numbers);

console.log(uniqueNumbers);

Output:

Set(5) {1,2,3,4,5}

Need an array again?

const result = [...uniqueNumbers];

console.log(result);

Output:

[1, 2, 3, 4, 5]

This is one of my favorite JavaScript tricks.


When Should You Use a Set?

Use a Set when:

  • You only want unique values.
  • You need to check if something exists quickly.
  • You want to remove duplicates.
  • The order is less important than uniqueness.

Use an array when:

  • Duplicates are allowed.
  • You need indexes like items[0].
  • You want methods like map(), filter(), or reduce().

Final Thoughts

Set is one of those features that feels simple, but once you start using it, you wonder how you lived without it.

Coming from Python, Set felt familiar right away. Before JavaScript added it, removing duplicates or checking unique values often meant writing extra code or helper functions. Now it's built in, clean, and easy to read.

Whenever I need unique values, I reach for Set first. It makes the code shorter, easier to understand, and less likely to have bugs.

javascriptweb-developmentbeginnerses6programming