You may call it a type if you’d rather, in terms of structural typing, but basically it’s just the built-in object with its own set of props and methods. As I understand it. Answer from udaaff on forum.freecodecamp.org
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Set
Set - JavaScript | MDN
August 13, 2026 - Takes a set and returns a new set containing elements in both this set and the given set.
🌐
W3Schools
w3schools.com › js › js_sets.asp
JavaScript Sets
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. ... // Create a Set const letters = new Set(); // Add Values to the Set letters.add("a"); ...
Discussions

What the heck the "new Set()" is?
It’s kinda returns an object, but objects have a pair prop: 'val', but ‘Set’ has only one prop/val(?)… When I do console.log(new Set([1,2,3,4,4,5,5,5,6])) in my console I have this result Set(5) {1, 2, 3, 4, 5,6}. As I remember it’s one of the ES6 feature but I’m not sure. More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
5
0
June 19, 2022
Ways to create a Set in JavaScript? - Stack Overflow
Sets are now available in ES2015 ... for JavaScript since June 2015. ECMAScript 6 has the data structure Set which works for arbitrary values, is fast and handles NaN correctly. -Axel Rauschmayer, Exploring ES6 · First two examples from Axel Rauschmayer's book Exploring ES6: ... > let set = new Set(); > ... More on stackoverflow.com
🌐 stackoverflow.com
New JavaScript Set methods
Excellent - finally we can use Set for more than a "duplicate-detector". I only wish that there were some way for objects to be "equal" without them having to be the exact same instance. Kind of like Java's .equals and .hashCode methods ... Well, I'll keep dreaming ... More on reddit.com
🌐 r/javascript
12
53
July 10, 2024
New JavaScript Set Methods
Maybe one self-contained example w/ a new function you can copy/paste into the console to play around with would be cool · I was mostly using it as a test to see if it worked in Chrome so I could start using it More on news.ycombinator.com
🌐 news.ycombinator.com
38
92
June 26, 2024
🌐
Telerik
telerik.com › blogs › understanding-utilizing-javascript-set-object
Understanding and Utilizing the JavaScript Set Object
February 2, 2024 - It provides an efficient way to ... primitive types, objects and even other sets! To create a new Set object, we can use the Set() constructor:...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Map › set
Map.prototype.set() - JavaScript | MDN
September 7, 2026 - The set() method of Map instances adds a new entry with a specified key and value to this Map, or updates an existing entry if the key already exists.
🌐
freeCodeCamp
forum.freecodecamp.org › curriculum help
What the heck the "new Set()" is? - Curriculum Help - The freeCodeCamp Forum
June 19, 2022 - It’s kinda returns an object, but objects have a pair prop: 'val', but ‘Set’ has only one prop/val(?)… When I do console.log(new Set([1,2,3,4,4,5,5,5,6])) in my console I have this result Set(5) {1, 2, 3, 4, 5,6}. As I remember it’s one of the ES6 feature but I’m not sure.
Find elsewhere
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Set › add
Set.prototype.add() - JavaScript | MDN
The Set object. js · const mySet = new Set(); mySet.add(1); mySet.add(5).add("some text"); // chainable console.log(mySet); // Set [1, 5, "some text"] Set · Set.prototype.delete() Set.prototype.has() Was this page helpful to you? Yes · No Learn how to contribute ·
Top answer
1 of 9
126

Sets are now available in ES2015 (aka ES6, i.e. ECMAScript 6). ES6 has been the current standard for JavaScript since June 2015.

ECMAScript 6 has the data structure Set which works for arbitrary values, is fast and handles NaN correctly. -Axel Rauschmayer, Exploring ES6

First two examples from Axel Rauschmayer's book Exploring ES6:

Managing single elements:

> let set = new Set();
> set.add('red')

> set.has('red')
true
> set.delete('red')
true
> set.has('red')
false

Determining the size of a Set and clearing it:

> let set = new Set();
> set.add('red')
> set.add('green')

> set.size
2
> set.clear();
> set.size
0

I would check out Exploring ES6 if you want to learn more about Sets in JavaScript. The book is free to read online, but if you would like to support the author Dr. Axel Rauschmayer you can purchase the book for around $30.

If you want to use Sets and ES6 now you can use Babel, the ES6 to ES5 transpiler, and its polyfills.

Edit: As of June 6th, 2017 most of the major browsers have full Set support in their latest versions (except IE 11). This means you may not need babel if you don't care to support older browsers. If you want to see compatibility in different browsers including your current browser check Kangax's ES6 compatibility table.

EDIT:

Just clarification on initialization. Sets can take any synchronous iterable in their constructor. This means they can take not just arrays but also strings, and iterators. Take for example the following array and string initialization of a set:

const set1 = new Set(['a','a','b','b','c','c']);
console.log(...set1);
console.log(set1.size);
const set2 = new Set("aabbcc");
console.log(...set2);
console.log(set2.size);

Both outputs of the array and string are the same. Note that ...set1 is the spread syntax. It appears that each element of the iterable is added one by one to the set, so since both the array and string have the same elements and since the elements are in the same order the set is created the same. Another thing to note about sets is when iterating over them the iteration order follows the order that the elements were inserted into the set. Here's an example of iterating over a set:

const set1 = new Set(['a','a','b','b','c','c']);
for(const element of set1) {
  console.log(element);
}

Since you can use any iterable to initialize a set you could even use a iterator from a generator function. Here is two such examples of iterator initializations that produce the same output:

// a simple generator example
function* getLetters1 () {
  yield 'a';
  yield 'a';
  yield 'b';
  yield 'b';
  yield 'c';
  yield 'c';
}

// a somewhat more commonplace generator example
// with the same output as getLetters1.
function* getLetters2 (letters, repeatTimes) {
  for(const letter of letters) {
    for(let i = 0; i < repeatTimes; ++i) { 
      yield letter;
    }
  }
}

console.log("------ getLetters1 ------");
console.log(...getLetters1());
const set3 = new Set(getLetters1());
console.log(...set3);
console.log(set3.size);

console.log("------ getLetters2 ------");
console.log(...getLetters2('abc', 2));
const set4 = new Set(getLetters2('abc', 2));
console.log(...set4);
console.log(set4.size);

These examples' generator functions could just be written to not repeat, but if the generator function is more complicated and as long as the following doesn't impact performance too negatively you could use the Set method to help get only values from a generator that don't repeat.

If you want to know more about sets without reading Dr. Rauschmayer's chapter of his book you can check out the MDN docs on Set. MDN also has more examples of iterating over a set such as using forEach and using the .keys, .values, and .entries methods. MDN also has examples such as set union, set intersection, set difference, symmetric set difference, and set superset checking. Hopefully most of those operations will become available in JavaScript without needing to build your own functions supporting them. In fact, there is this TC39 proposal for new Set methods which should hopefully add the following methods to Set in JavaScript at some future point in time if the proposal reaches stage 4:

  • Set.prototype.intersection(iterable) - method creates new Set instance by set intersection operation.
  • Set.prototype.union(iterable) - method creates new Set instance by set union operation.
  • Set.prototype.difference(iterable) - method creates new Set without elements present in iterable.
  • Set.prototype.symmetricDifference(iterable) - returns Set of elements found only in either this or in iterable.
  • Set.prototype.isSubsetOf(iterable)
  • Set.prototype.isDisjointFrom(iterable)
  • Set.prototype.isSupersetOf(iterable)
2 of 9
32

I use dict objects as sets. This works with strings and numbers, but I suppose would cause problems if you wanted to have a set of objects using custom equality and comparison operators:

Creating a set:

var example_set = 
{
    'a':true,
    'b':true,
    'c':true
}

Testing for inclusion in a set

if( example_set['a'] ){
    alert('"a" is in set');
}

Adding an element to a set

example_set['d'] = true;

Removing an element from a set

delete example_set['a'];

🌐
GeeksforGeeks
geeksforgeeks.org › javascript › sets-in-javascript
Set in JavaScript - GeeksforGeeks
A set can be created either empty or by providing an iterable like array or string. JavaScript · // using an array let s1 = new Set([10, 30, 30, 40, 40]); console.log(s1); let s2 = new Set(["gfg", "gfg", "geeks"]); console.log(s2); // using string let s3 = new Set("fooooooood"); console.log(s3); // an empty set let s4 = new Set(); console.log(s4); Output ·
Published: September 25, 2025
🌐
DEV Community
dev.to › keevcodes › the-javascript-set-object-2f75
The JavaScript Set Object - DEV Community
June 27, 2019 - Set is an object that can store any type of JavaScript primitive (think booleans, strings, numbers here), but it will only store unique primitives. Wait... what??? That's awesome!
🌐
Reddit
reddit.com › r/javascript › new javascript set methods
r/javascript on Reddit: New JavaScript Set methods
July 10, 2024 - Learn about sets, how you can use these methods to compare different sets, create new sets with specific properties, and more.
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › data types
Map and Set
A Set is a special type collection ... only once. ... new Set([iterable]) – creates the set, and if an iterable object is provided (usually an array), copies values from it into the set....
🌐
Hacker News
news.ycombinator.com › item
New JavaScript Set Methods | Hacker News
June 26, 2024 - Maybe one self-contained example w/ a new function you can copy/paste into the console to play around with would be cool · I was mostly using it as a test to see if it worked in Chrome so I could start using it
🌐
W3Schools
w3schools.com › jsref › jsref_set_new.asp
JavaScript new Set Method
Full JavaScript Set Reference · Create an empty set and add values: // Create a Set const letters = new Set(); // Add Values to the Set letters.add("a"); letters.add("b"); letters.add("c"); Try it Yourself » · Set is an ECMAScript6 (ES6 2015) feature. JavaScript 2015 is supported in all browsers since June 2017: ❮ Previous JavaScript Set Reference Next ❯ ·
🌐
Medium
noncodersuccess.medium.com › exploring-javascript-sets-practical-use-cases-and-methods-demystified-deep-dive-into-set-c16a5c5c883f
Exploring JavaScript Sets | Practical Use Cases and Methods Demystified | Deep Dive into Set Methods | JavaScript Set Mastery | by NonCoderSuccess | Medium
April 29, 2024 - We’ve covered a lot of ground, from understanding what Sets are to mastering their various methods for manipulating data. As you’ve seen, JavaScript Sets offer a versatile and efficient way to work with collections of unique values.
Top answer
1 of 1
8

Set can only contain unique values; here is what constitutes an equal value according to MDN:

Because each value in the Set has to be unique, the value equality will be checked. In an earlier version of ECMAScript specification, this was not based on the same algorithm as the one used in the === operator. Specifically, for Sets, +0 (which is strictly equal to -0) and -0 were different values. However, this was changed in the ECMAScript 2015 specification. See "Key equality for -0 and 0" in the browser compatibility table for details.

NaN and undefined can also be stored in a Set. All NaN values are equated (i.e. NaN is considered the same as NaN, even though NaN !== NaN).

There is an implicit loop in creation of the Set, using the iterable interface of the passed array. There is also a loop in ..., which unpacks an iterable.

However, checking whether an element is already in Set is not done using a loop - for all intents and purposes you can think of a Set as a dictionary with only keys, and no values. The code is almost the same as:

const ages = [26, 27, 26, 26, 28, 28, 29, 29, 30]

const temp = {};
for (const age of ages) {
  temp[age] = true;
}

const uniqueAges = [];
for (const age in temp) {
  uniqueAges.push(age);
}

console.log(uniqueAges);
Run code snippetEdit code snippet Hide Results Copy to answer Expand

I am intentionally writing this "low tech" way to illustrate what is going on; notice that object keys are always strings so the result will be stringified. Using a Map or also storing ages as values would make it much more similar in effect to OP's code, but I wanted to keep it simple for the sake of illustration.

🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Set › values
Set.prototype.values() - JavaScript | MDN
July 20, 2025 - const mySet = new Set(); mySet.add("foo"); mySet.add("bar"); mySet.add("baz"); const setIter = mySet.values(); console.log(setIter.next().value); // "foo" console.log(setIter.next().value); // "bar" console.log(setIter.next().value); // "baz"
🌐
Medium
medium.com › @TomHenricksen › javascript-sets-creating-using-and-iterating-15f11eca5790
JavaScript Sets: Creating, Using, and Iterating | by Tom Henricksen | Medium
May 13, 2025 - To declare a set in JavaScript is quite simple. Here is a set of cars declared. ... We have three elements that are Strings in this set. What if we put a value in twice? A Set will take care of duplicates for you. It will remove the repeated values. Here is an example where we have added Ford twice. const cars = new Set(["Ford", "Chevy", "Honda", "Ford"]); console.log(cars);
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-set-constructor
JavaScript Set() Constructor - GeeksforGeeks
July 23, 2025 - The Javascript Set constructor is used to create Set objects. It will create a set of unique values of any type, whether primitive values or object preferences. It returns the new Set object.